简体   繁体   中英

Why Request doesn't reach to controller in spring mvc?

I try many examples to find out why requests doesn't reach to my controller,but it fails. when i do start my application it'll show homepage (index.jsp) but when i fill the form and press submit button it gives me 404 not found error! here is my files :

Index.jsp (HomePage)

<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>

dispatcher-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>
 <form method="post" action="/form">

将上面的行更改为此

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

Try changing the servlet mapping for the dispatcher servlet as follows:

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

(Note the /* )

Refer to Difference between / and /* in servlet mapping url pattern for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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