2016-05-15 86 views
2

私はSpring MVCでプログラムを実行しようとしていますが、以下はそのコードです。私は、プログラムを実行しようとすると、私はエラーが表示さNo mapping found for HTTP request with URI [/HelloWeb/] in DispatcherServlet with name 'HelloWeb' ?:と404エラーページが表示されます、私はこの問題を解決することができますどのように?...実際のプログラムコードはhttp://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm名前が 'HelloWeb'のDispatcherServletのURI [/ HelloWeb /]を持つHTTP要求のマッピングが見つかりませんでしたか?

のWeb.xml

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Spring MVC Form Handling</display-name> 

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

でありますHelloWeb-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 

    <context:component-scan base-package="com.SpringMVC" /> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

StudentController.java

package com.SpringMVC; 

import org.springframework.stereotype.Controller; 
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; 
import org.springframework.ui.ModelMap; 

@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("SpringWeb")Student student, 
    ModelMap model) { 
     model.addAttribute("name", student.getName()); 
     model.addAttribute("age", student.getAge()); 
     model.addAttribute("id", student.getId()); 

     return "result"; 
    } 
} 

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> 

答えて

2

あなたの要求パスが正しくありません。以下試してみてください;)

@Controller 
@RequestMapping(value = "/HelloWeb") 
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("SpringWeb")Student student, 
    ModelMap model) { 
     model.addAttribute("name", student.getName()); 
     model.addAttribute("age", student.getAge()); 
     model.addAttribute("id", student.getId()); 

     return "result"; 
    } 
+0

は、上記のコードの変更を行ったが、それはまだ同じエラーがスローされますように、あなたのコントローラのコードで/HelloWebを追加<form:form method="POST" action="/addStudent">

それとも

よう<form:form method="POST" action="/HelloWeb/addStudent">から/HelloWebを削除しますコンソール。 – Kriz

+0

その2つの方法の両方? – Blank

+0

はい、どちらも2つの方法です。 – Kriz

関連する問題