我们如何从Spring Boot控制器重定向到所需的角度页面(路由页面)-将角度集成在Spring Boot中 [英] How can we redirect to required angular page (routing page) from spring boot controller - angular integrated inside spring boot

查看:20
本文介绍了我们如何从Spring Boot控制器重定向到所需的角度页面(路由页面)-将角度集成在Spring Boot中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Spring Boot应用程序中集成了角度应用程序...例如,角度构建文件被放置在Spring Boot应用程序的静态文件夹中,如下图所示。

I have defined two angular routing urls like

 1. http://localhost:8080/pageone
 2. http://localhost:8080/pagetwo

当我从浏览器访问上述urls时,are handled by server(Spring启动应用程序)和not by the angular。所以I end up in page not found

We can redirect to index page像这样从弹簧靴

@GetMapping("/")
public RedirectView welcome(RedirectAttributes attributes) {
    //attributes.addFlashAttribute("flashAttribute", "redirectWithRedirectView");
    //attributes.addAttribute("pageToRequest", "paymentoverview");
    return new RedirectView("index.html");
}

but我们可以only redirect to index.htmlnot to routing urls "/pageone" or "/pagetwo"

我不想在索引页结束。

Somehow I wan't to end up in respective page that I accessed from browser automatically, 
even after redirect to index page also fine.

我尝试将属性与index.html一起发送,但不起作用。 我们如何解决此问题。

推荐答案

我找到了解决方案..

从浏览器访问以下URL

1. http://localhost:8080/pageone
2. http://localhost:8080/pagetwo

请求将直接到达弹簧控制器,并由下面的控制器处理

@Controller // should not be @RestController
public class BaseController implements ErrorController { // implement ErrorController to handle 404 (error pages)

    // Always redirect to index.html page (only option)

    // Can handle all request (which end up in 404)
    @GetMapping("/error")
    public String error() {
        return "forward:/index.html";
    }

    // Specific request 
    @GetMapping("/pageone")
    public String pageone() {
        return "forward:/index.html";
    }

    // Specific request with pathvariable and requestparam
    @GetMapping("/pagetwo" + "/{id}")
    public String pagetwo(@PathVariable("id") String id,
                                  @RequestParam("param1") String param1,
                                  @RequestParam("param2") String param2 ) {
        // both pathvariable and requestparam will be sent to front end
        return "forward:/index.html";
    }
}

索引页将在角度自动加载请求页之后调度,例如:‘/pageone’或‘pagetwo’

app-routing.modes.ts

const routes: Routes = [
  {
    path: 'pageone',
    component: PageOneComponent
  },
  {
    path: 'pagetwo/:id', // to handle pathvariables
    component: PageTwoComponent
  },
  {
    path: '**',
    component: HomeComponent
  }
  ]

您可以访问收到的路径变量和请求参数,如下所示

PageTwoComponent.ts

  ngOnInit(): void {
    console.log(this.route.snapshot.paramMap.get('id')); // get pathparms
    this.route.queryParams.subscribe(params => { // get queryparams
      console.log(params['param1']);
      console.log(params['param2']);
  });
  }

简而言之,@Controllerimplements ErrorController

@GetMapping("/error")
public String error() {
   return "forward:/index.html";
}

After that angular automatically load requested page (routing url)

这篇关于我们如何从Spring Boot控制器重定向到所需的角度页面(路由页面)-将角度集成在Spring Boot中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆