[英]Page not found error in spring mvc 4 although it exists
I am developing a small application using spring mvc 4 which gets the input from user and inserts it into the database. 我正在使用spring mvc 4开发一个小型应用程序,该应用程序从用户那里获取输入并将其插入数据库。 Here is the dispatcher-servlet.xml
这是dispatcher-servlet.xml
<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="controllers.FormController" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
And here is my controller 这是我的控制器
@Controller
@RequestMapping("/form")
public class FormController {
@Autowired
private FormService formService;
public FormService getFormService() {
return formService;
}
public void setFormService(FormService formService) {
this.formService = formService;
}
@RequestMapping(value = "/userForm", method = RequestMethod.GET)
public String showUser(Model model){
User user = new User();
model.addAttribute("userForm", user);
return "index";
}
@RequestMapping(value = "/adduser", method = RequestMethod.POST)
public String addUser(@ModelAttribute("userForm") User user, BindingResult result, Model model){
formService.insert(user.getUsername(), user.getPassword());
return "success";
}
}
When i type the following url in browser " http://localhost:8080/FormSpring/form/userForm.htm ", it shows me the page not found error, although the success page is located in the WEB-INF/jsp directory 当我在浏览器“ http:// localhost:8080 / FormSpring / form / userForm.htm ”中键入以下URL时,尽管成功页面位于WEB-INF / jsp目录中,但它向我显示页面未找到错误
Here is the web.xml file 这是web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
Also, the structure of the project is shown in the image bellow 此外,该项目的结构如下图所示
Your controller is mapped to adduser, not adduser.htm! 您的控制器映射到adduser,而不是adduser.htm! Also in the browser you perform a GET, not a POST request.
同样,在浏览器中,您将执行GET而不是POST请求。
Give the endpoint as of controller, insted of jsp page Url should be: http://localhost:8080/FormSpring/form/adduser
给出从控制器开始的端点,插入jsp页面的Url应该是:
http://localhost:8080/FormSpring/form/adduser
Make sure of proper Request type POST/GET
确保正确的请求类型
POST/GET
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.