2013-05-28 5 views
0
.

. Пробую пример из www.tutorialspoint.com для обработки формы Spring MVC. Я получаю следующее при нажатии кнопки отправки на странице при работе в браузере:Ошибка 404 при отправке формы весной. Веб-приложение MVC.

HTTP Status 404 - /HelloWeb/addStudent 

type Status report 

message /HelloWeb/addStudent 

description The requested resource is not available. 


Это веб .xml файл:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 

    <display-name>NProject</display-name> 

    <welcome-file-list> 
     <welcome-file>student.jsp</welcome-file> 
    </welcome-file-list> 

    <servlet> 
     <servlet-name>HelloWeb</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>HelloWeb</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 


Servlet конфигурационный файл is HelloWeb-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-3.0.xsd 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-3.0.xsd 
         http://www.springframework.org/schema/mvc 
         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <context:component-scan base-package="bundle" /> 

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basename" value="/WEB-INF/messages" /> 
     <property name="cacheSeconds" value="3000" /> 
    </bean> 

</beans> 


файл Контроллер:

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class StudentCOntroller { 

    @RequestMapping(value = "/student", method = RequestMethod.GET) 
    public ModelAndView student(){ 
     return new ModelAndView("student", "command", new Student()); 
    } 

    @RequestMapping(value = "/addstudent", method = RequestMethod.POST) 
    public String addStudent(@ModelAttribute("student") Student student, 
          ModelMap model, 
          BindingResult result) { 
     model.addAttribute("name",student.getName()); 
     model.addAttribute("age",student.getAge()); 
     model.addAttribute("id",student.getId()); 

     return "result"; 
    } 
} 


Это JSP файлы:

student.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<head> 
    <title>Spring MVC Form Handling</title> 
</head> 
<body> 
    <h2>Student Information</h2> 
    <form:form method="POST" action="/HelloWeb/addStudent"> 
     <table> 
      <tr> 
       <td><form:label path="name">Name</form:label></td> 
       <td><form:input path="name" /></td> 
      </tr> 
      <tr> 
       <td><form:label path="age">Age</form:label></td> 
       <td><form:input path="age" /></td> 
      </tr> 
      <tr> 
       <td><form:label path="id">id</form:label></td> 
       <td><form:input path="id" /></td> 
      </tr> 
      <tr> 
       <td colspan="2"> 
        <input type="submit" value="Submit"/> 
       </td> 
      </tr> 
     </table> 
    </form:form> 
</body> 
</html> 


result.jsp:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<html> 
<head> 
    <title>Spring MVC Form Handling</title> 
</head> 
<body> 
    <h2>Submitted Student Information</h2> 
    <table> 
     <tr> 
      <td>Name</td> 
      <td>${name}</td> 
     </tr> 
     <tr> 
      <td>Age</td> 
      <td>${age}</td> 
     </tr> 
     <tr> 
      <td>ID</td> 
      <td>${id}</td> 
     </tr> 
    </table> 
</body> 
</html> 


Пожалуйста, помогите мне понять эту проблему.
Спасибо заранее.

ответ

1

Изменить действие от student.jsp к

<form:form method="POST" action="addStudent"> 

Он должен работать.

+0

В случае с ФП в ваше предложение не имеет никакого значения. См. Ссылку на учебник, о котором говорит OP в теле вопроса. – informatik01

+0

** Большое спасибо друзьям ** – user2083175

+0

@ user2083175 Так в чем была причина? Использование 'action =" addStudent "' вместо 'action ="/HelloWeb/addStudent "' это ** определенно не ** причина. Так что это было? – informatik01

0

Определите комплект поставки в вашем контроллере.

package bundle; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 
0

В вашем student.jsp, измените строку

<form:form method="POST" action="addStudent"> 

Он работает

Смежные вопросы