/**
* SpringMVC 注解参数的使用
*
* 1、@RequestParam配置请求参数,可获得对应参数值
* 2、@RequestHeader配置请求头
* 3、@CookieValue配置请求cookie
* 4、User 可以传入普通java类,支持级联属性赋值
* 5、支持原声servletapi/writer /session等servlet常用类
*
*
*
* @param name
* @return
*/
测试类:
package com.spring.controller;
import java.io.Writer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.spring.vo.User;
@Controller
public class TestController {
/**
* SpringMVC 注解参数的使用
*
* 1、@RequestParam配置请求参数,可获得对应参数值
* 2、@RequestHeader配置请求头
* 3、@CookieValue配置请求cookie
* 4、User 可以传入普通java类,支持级联属性赋值
* 5、支持原声servletapi/writer /session等servlet常用类
*
*
*
* @param name
* @return
*/
//http://127.0.0.1:8080/26SpringMVC03/testRequestParam?name=abc
@RequestMapping(value="testRequestParam")
public String testRequestParam(@RequestParam(value="name",required=false ) String name ){
System.out.println("name == "+name);
return "success";
}
//http://127.0.0.1:8080/26SpringMVC03/testRequestHeader
@RequestMapping(value="testRequestHeader")
public String testRequestHeader(@RequestHeader(value="Accept-Language") String language){
System.out.println("language == "+language);
return "success";
}
//http://127.0.0.1:8080/26SpringMVC03/testCookie
@RequestMapping(value="testCookie")
public String testCookie(@CookieValue(value="JSESSIONID") String jessionID){
System.out.println("jessionID == "+jessionID);
return "success";
}
//http://127.0.0.1:8080/26SpringMVC03/testModel?id=1&userName=test&equire.name=sword&equire.attack=500
@RequestMapping(value="testModel")
public String testModel(User user){
System.out.println(user);
return "success";
}
//http://127.0.0.1:8080/26SpringMVC03/testServlet?name=123
@RequestMapping(value="testServlet")
public String testServlet(HttpServletRequest request,HttpSession session, Writer writer){
System.out.println(request);
System.out.println(session);
System.out.println(request.getParameter("name"));
return "success";
}
}
user类:
package com.spring.vo;
import org.springframework.stereotype.Component;
@Component
public class User {
private int id;
private String userName;
private Equirement equire;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String userName, Equirement equire) {
super();
this.id = id;
this.userName = userName;
this.equire = equire;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Equirement getEquire() {
return equire;
}
public void setEquire(Equirement equire) {
this.equire = equire;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", equire=" + equire + "]";
}
}
equirement:
package com.spring.vo;
import org.springframework.stereotype.Component;
@Component
public class Equirement {
private String name;
private int attack;
public Equirement() {
super();
// TODO Auto-generated constructor stub
}
public Equirement(String name, int attack) {
super();
this.name = name;
this.attack = attack;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
@Override
public String toString() {
return "Equirement [name=" + name + ", attack=" + attack + "]";
}
}
spring-mvc.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package="com.spring"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>25SpringMVC02</display-name>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- The front controller of this Spring Web application, responsible for
handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>