2016-04-23 49 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这是空的。 –

相关问题