我嘗試了很多例子來找出爲什麼請求沒有達到我的控制器,但它失敗了。 當我開始我的應用程序時,它會顯示主頁(index.jsp),但是當我填寫表單並按提交按鈕,它會給我404未找到錯誤! 這裏是我的文件:爲什麼請求在spring mvc中沒有到達控制器?
的index.jsp(主頁)
<html>
<body>
<form method="post" action="/form">
<input type="text" name="name"/>
<input type="submit"/>
</form>
</body>
</html>
StudentController.java
package rankbooster.ir.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
/**
* Created by Mohammad Reza Khatami on 4/23/2016.
*/
@Controller
public class StudentController
{
@RequestMapping(value = "/form",method = RequestMethod.POST)
public String getFormData(@RequestParam("name") String name, Model model)
{
model.addAttribute("name",name);
return "index2";
}
}
index2.jsp
<html>
<body>
<b>${name}</b>
<b>${family}</b>
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.1">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
調度-servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="rankbooster.ir.controller"/>
<context:annotation-config />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
是你在/ WEB-INF/pages /'目錄下的'index2.jsp'嗎? –
是的,它是在/ WEB-INF /頁/ –
什麼是你的應用程序部署在 –