简体   繁体   English

如何在Spring MVC中使用Controller和Request Mapping修复“找不到” html?

[英]How to fix “Not Found” html using Controller and Request Mapping in Spring MVC?

First of all I don't know if my question is the correct question to as so I'm just going to explain it. 首先,我不知道我的问题是否是正确的问题,因此我将只作解释。 I'm new to Spring MVC and I'm trying an example where I will click on a text and redirect it to hello.html and try out the controller but for some reason even though I created the Controller with Request Mapping it won't work or it will not found the file. 我是Spring MVC的新手,我正在尝试一个示例,在该示例中,我将单击文本并将其重定向到hello.html并尝试使用控制器,但是由于某种原因,即使我使用请求映射创建了控制器,它也不会工作,否则将找不到该文件。

Here is my controller HelloWorldController in the com.springapp.controllers package: 这是com.springapp.controllers包中的控制器HelloWorldController

 package com.springapp.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloWorldController { @RequestMapping("/hello") public ModelAndView hello() { String message = "Hajimemashite!"; return new ModelAndView("hello", "message", message); } } 

And in my dispatcher-servlet.xml I have a line of code: 在我的dispatcher-servlet.xml中 ,有一行代码:

 <context:component-scan base-package="com.springapp.controllers" /> 

Here is my index.jsp 这是我的index.jsp

 <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Spring App Try</title> </head> <body> <a href="hello.html">Click Here</a> </body> </html> 

and in my hello.jsp 在我的hello.jsp中

 <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> ${message} </body> </html> 

I don't know if I'm doing it properly. 我不知道我做得是否正确。 Can someone help me and enlighten me if I'm doing it wrong. 如果我做错了,有人可以帮助我并启发我。 So far I've tried replacing @RequestMapping("/hello") in my HelloWorldController with @RequestMapping(value="/hello") but when I tried clicking the "Click Here" the same result: 到目前为止,我已经尝试将我的HelloWorldController中的@RequestMapping("/hello")替换为@RequestMapping(value="/hello")但是当我尝试单击“ Click Here”时,结果相同:

Result When Clicking the "Click Here" 单击“单击此处”时的结果

I also tried replacing the hello.jsp to hello.html but it still doesn't work. 我还尝试将hello.jsp替换为hello.html,但仍然无法正常工作。

尝试使用http://localhost:8080/Spring_HelloWord_App/hello您尝试使用的http://localhost:8080/Spring_HelloWord_App/hello.html不会映射到任何控制器方法。

As mentioned by @shazin you need to set the url to /hello instead of hello.html. 如@shazin所述,您需要将URL设置为/ hello而不是hello.html。 See that it invokes your controller. 看到它调用了您的控制器。 You will also need to configure your view resolver to locate your hello.jsp file, after controller is invoked. 在调用控制器之后,您还需要配置视图解析器以定位hello.jsp文件。 Refer ViewResolver 请参阅ViewResolver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM