2016-11-18 2 views
3

Ich habe einen dougr über diese Operation. diese Klasse Controller Erstellen funktioniert:GetMapping und PostMapping Annotationen

@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"; 
} 

Aber mit Getmapping und PostMapping funktioniert nicht, das Projekt läuft normalerweise, aber nicht die Seite Anruf angezeigt werden soll. Ie. :

@Controller 
    public class StudentController { 

    @GetMapping("/student") 
    public ModelAndView student() { 
    return new ModelAndView("student", "command", new Student()); 
    } 

    @PostMapping("/addStudent") 
    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"; 
    } 
} 

-Code voll ist, die Art und Weise funktioniert unten richtig:

Student.java

public class Student { 

    private Integer age; 
    private String name; 
    private Integer id; 

    public Integer getAge() { 
     return age; 
    } 
    public void setAge(Integer age) { 
     this.age = age; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public Integer getId() { 
     return id; 
    } 
    public void setId(Integer id) { 
     this.id = id; 
    } 
} 

StudentController

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
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("SpringWeb") Student student, ModelMap model) { 
     model.addAttribute("name", student.getName()); 
     model.addAttribute("age", student.getAge()); 
     model.addAttribute("id", student.getId()); 
     return "result"; 
    } 
} 

result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
     pageEncoding="UTF-8"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<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> 

student.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Spring MVC Form Handling</title> 
</head> 
<body> 

    <h2>Student Information</h2> 
    <form:form method="POST" action="/HelloForm/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> 

SpringContext.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 

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

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

</bean> 

</beans> 

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 
    <display-name>HelloForm</display-name> 

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

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

</web-app> 
+1

Welche Federversion verwendest du ?! – KLajdPaja

+0

Ich benutze Frühling 4.3.4. –

+0

Bitte geben Sie eine [mcve] an und erläutern Sie genau die Anforderung, die Sie stellen, das Verhalten, das Sie erwarten, und das Verhalten, das Sie tatsächlich erhalten. –

Antwort

1

In Ihrem SpringContext.xml Datei, Sie Klasse DefaultAnnotationHandlerMapping verwenden (das war seit Frühling 3.2 veraltet), um @Reques zu handhaben Annotation implizit hinzufügen, die die neue Annotation wie @GetMapping, @PostMapping ... (die seit der Version 4.3 in Spring eingeführt wurde) nicht versteht, ignoriert also die neuen Annotationen. Um die neue Anmerkung zu verwenden, sollten Sie RequestMappingHandlerMapping Klasse verwenden.

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> 
<!-- you also need to define an adapter, otherwise you will get a ServletException while handling the request --> 
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 

Aber die empfohlene Methode ist die „MVC XML-Namespace“ zu verwenden, wird es über die Arbeit für Sie tun: Sie können einfach diese Bohne in Ihrem SpringContext.xml wie folgt definieren. Fügen Sie den folgenden Code in Ihre SpringContext.xml (Sie mvc Namespace definieren sollte zuerst):

<mvc:annotation-driven/> 

Bitte sehen here, um weitere Informationen über diese.

0

Sie können den @Controller zu @RestController ändern und dann @GetMapping und @PostMapping verwenden und den Code ausführen. Das sollte funktionieren.

Verwandte Themen