GetMapping和PostMapping注释

时间:2016-11-18 15:09:53

标签: spring spring-mvc

我有关于此操作的doubr。创建此类控制器有效:

server.session.cookie.http-only= # "HttpOnly" flag for the session cookie. configuration property.

但是使用GetMapping和PostMapping不起作用,项目正常运行,但不显示页面调用。 IE浏览器。 :

@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.java

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

StudentController

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

result.jsp中

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

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

SpringContext.xml

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

的web.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>

3 个答案:

答案 0 :(得分:1)

SpringContext.xml 文件中,使用 DefaultAnnotationHandlerMapping 类(自Spring 3.2以来不推荐使用)来隐式处理@RequestMapping注释,这是不明白的像@GetMapping,@ PostMapping ......这样的新注释(自4.3版开始引入Spring),因此它忽略了新的注释。要使用新注释,您应该使用 RequestMappingHandlerMapping 类。您可以在SpringContext.xml中简单地定义此bean,如下所示:

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

但推荐的方法是使用&#34; MVC XML命名空间&#34;,它将为您完成上述工作。将以下代码添加到SpringContext.xml中(您应该首先定义mvc名称空间):

<mvc:annotation-driven/>

有关详细信息,请参阅here

答案 1 :(得分:1)

您可以将@Controller更改为@RestController,然后使用@GetMapping和@PostMapping运行代码。这应该有用。

答案 2 :(得分:0)

匿名以上悲伤正确,将@Controller更改为@RestController。这是另一个决定,这是第一个决定-在@GetMapping或@PostMapping之后添加@ResponseBody。