2016-04-23 48 views
0

我嘗試了很多例子來找出爲什麼請求沒有達到我的控制器,但它失敗了。 當我開始我的應用程序時,它會顯示主頁(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> 
+0

是你在/ WEB-INF/pages /'目錄下的'index2.jsp'嗎? –

+0

是的,它是在/ WEB-INF /頁/ –

+0

什麼是你的應用程序部署在 –

回答

0
<form method="post" action="/form"> 

變化上面的行到這個

<form method="post" action="form"> 
+0

它不會改變。 –

+0

bt你必須改變這一點。 – Priyamal

0

試着改變servlet映射對於調度的servlet如下:

<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

(注意/*

參考Difference between/and /* in servlet mapping url pattern瞭解詳情。

+0

當我這樣做時,我無法看到我的主頁,因爲404錯誤代碼(web文件夾下)和localhost:8080/form不能找到404。 –

+0

好的,那麼你似乎有一個額外的問題,我目前沒有看到。也許你可以增加DispatcherServlet的日誌級別以獲得更多的洞察力? –

+0

在dispatcher-servlet.xml導航到控制器是好的(CTRL +單擊),我也有applicationContext.xml這是空的。 –

相關問題