Angular 4 and Spring boot - 404 page not found

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

need your help here please.

I have a login page. After I enter username/password, I want to see Dashboard page. But I am getting 404 page not found. Can anyone please tell what is going on here.

When I hit http://localhost:8080 -> It goes to http://localhost:8080/login - Which is expected.

After I enter username/password, it goes to http://localhost:8080 - Expected: to go to Dashboard page i.e. http://localhost:8080/dashboard

@Component public class SimpleAuthenticationSuccessHandler implements AuthenticationSuccessHandler {  @Override     public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {        ...         redirectStrategy.sendRedirect(request, response, "dashboard");     } 

DashboardController.java

@Controller @RequestMapping("/dashboard") public class DashboardController {      @RequestMapping("/")     String init(){         System.out.println("Dashboard - init()");         return "dashboard_init";     } } 

app.component.html

Hello... {{name}}   Hello... {{title}} <h1>     Welcome {{title}}!   </h1> <p>Id: <span>{{greeting.id}}</span></p>     <p>Message: <span>{{greeting.content}}!</span></p> 

app-routing.module.ts

import {DashboardComponent} from "./dashboard/dashboard.component"; const routes: Routes = [     { path: '', redirectTo: '/login', pathMatch: 'full' },  //   { path: 'dashboard', redirectTo: '/dashboard', pathMatch: 'full' },     {         path: 'dashboard_init',         component: DashboardComponent,         data: { title: 'Dashboard' }     } ]; 

dashboard.component.ts

@Component({     selector: 'dashboard-component',     templateUrl: './dashboard.component.html',     styleUrls: ['./dashboard.component.css'], })  export class DashboardComponent implements OnInit {     private currentAssociate: Associate;      constructor(private http: Http,                 private router: Router) {     }      ngOnInit(): void {         // initialize services and data         this.http             .get('/dashboard')             .toPromise()             .then(response => {                 let data = response.json();                  if (data.currentAssociate) this.currentAssociate = data.currentAssociate as Associate;             })             .catch(error => {              //   this.alertService.error(error);             });     } } 

dashboard.component.html

<html xmlns="http://www.w3.org/1999/xhtml"      xmlns:th="http://www.thymeleaf.org"      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"     xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">  <head> <meta charset="utf-8" /> <title>Dashboard</title>  </head>  <div> <B>Dashboard...</B> </div> </html> 

Error: (When the url is http://localhost:8080/dashboard/)

Dashboard - init() [2m2018-03-26 10:07:20.421[0;39m [31mERROR[0;39m [35m13184[0;39m [2m---[0;39m [2m[nio-8080-exec-2][0;39m [36morg.thymeleaf.TemplateEngine            [0;39m [2m:[0;39m [THYMELEAF][http-nio-8080-exec-2] Exception processing template "dashboard_init": Error resolving template "dashboard_init", template might not exist or might not be accessible by any of the configured Template Resolvers [2m2018-03-26 10:07:20.422[0;39m [31mERROR[0;39m [35m13184[0;39m [2m---[0;39m [2m[nio-8080-exec-2][0;39m [36mo.a.c.c.C.[.[.[/].[dispatcherServlet]   [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "dashboard_init", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause  org.thymeleaf.exceptions.TemplateInputException: Error resolving template "dashboard_init", template might not exist or might not be accessible by any of the configured Template Resolvers     at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:246)     at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)     at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)     at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) 

回答1:

something wrong overall.. in Controller replace top /dashboard with /

@Controller @RequestMapping("/") public class DashboardController {     @RequestMapping("/")    String init(){        System.out.println("Dashboard - init()");       return "dashboard_init";    } } 

Also as fas as I remember return "dashboard_init" is expecting dashboard_init.html template to be returned

Probably you want redirect or something to /dashboard_init, do like

@RequestMapping(value = "/", method = RequestMethod.GET) public void index(final HttpServletResponse response) {     response.setStatus(HttpStatus.OK.value());     response.sendRedirect( "/wherever-you-want"); } 


回答2:

This series of tutorials will help you to integrate Spring Security with Angular, using different approaches. Starting with Basic Auth

Spring Security and Angular



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!