SpringMVC获取参数信息


过滤器(防止中文乱码)

package com.java.demo1.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class CharsetFilter
 */
@WebFilter("/CharsetFilter")
public class CharsetFilter implements Filter {

    
	public void destroy() {
		
	}

	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		//转型
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse =(HttpServletResponse) response;
		
		//设置乱码
		httpRequest.setCharacterEncoding("utf-8");
		httpResponse.setContentType("text/html;charset=utf-8");
		
		//放行
		chain.doFilter(httpRequest, httpResponse);
	}

	
	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}

然后在web.xml里面配置一下过滤范围;/*过滤所有

<filter>
  	<filter-name>char</filter-name>
  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  	<init-param>
  		<param-name>encoding</param-name>
  		<param-value>utf-8</param-value>
  	</init-param>
  </filter>
  
  
  <filter-mapping>
  	<filter-name>char</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>

一、定义参数

1、控制层

package com.java.demo1.controller;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.java.demo1.domain.User;

//普通类被用Controller注释就变成了servlet
@Controller
@RequestMapping(value="/aa")
@Scope("prototype") //控制器一般都要使用多例,单例会影响效率
public class ControllerDemo1 {
	
	//定义参数:获取参数信息
	@RequestMapping(value ="/register.action",method=RequestMethod.POST)
	public void register(String username,String password){
		System.out.println(username+","+password);
	}
}

2、jsp

<%@ page language="java" contentType="text/html; charset=Utf-8"
    pageEncoding="Utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<form action="aa/register.action" method="post">
		用户名:<input type="text" name="username">
		密码:<input type="password" name="password">
		<input type="submit" value="提交">
	</form>
</body>
</html>

二、对象接收

1、User

package com.java.demo1.domain;

import java.util.Date;

public class User {
	private String username;
	private String password;
	private String hobby;
	private Date birthday;//支持这样的格式:1988/09/09;一般可以采用String来进行接收
	private double score;//基础数据类型可以自动转换
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	
}

2、控制层

package com.java.demo1.controller;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.java.demo1.domain.User;

//普通类被用Controller注释就变成了servlet
@Controller
@RequestMapping(value="/aa")
@Scope("prototype") //控制器一般都要使用多例,单例会影响效率
public class ControllerDemo1 {
	
	//对象接收
	@RequestMapping(value ="/register2.action",method=RequestMethod.POST)
	public void register2(User user){
		System.out.println(user.getUsername()+","+user.getPassword()+","+user.getHobby()+","+user.getBirthday()+","+user.getScore());
	}

}

3、jsp

<%@ page language="java" contentType="text/html; charset=Utf-8"
    pageEncoding="Utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<form action="aa/register2.action" method="post">
		用户名:
		<input type="text" name="username">
		密码:
		<input type="password" name="password">
		爱好:
		<input type="checkbox" name="hobby" value="吃饭">吃饭
		<input type="checkbox" name="hobby" value="睡觉">睡觉
		<input type="checkbox" name="hobby" value="打豆豆">打豆豆
		出生年月:
		<input type="text" name="birthday">
		分数:
		<input type="text" name="score">
		<input type="submit" value="提交">
	</form>
</body>
</html>

三、当参数名和对象名不一致时

1、User

package com.java.demo1.domain;

import java.util.Date;

public class User {
	private String username;
	private String password;
	private String hobby;
	private Date birthday;//支持这样的格式:1988/09/09;一般可以采用String来进行接收
	private double score;//基础数据类型可以自动转换
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	
}

2、控制层

package com.java.demo1.controller;

import java.util.Date;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.java.demo1.domain.User;

//普通类被用Controller注释就变成了servlet
@Controller
@RequestMapping(value="/aa")
@Scope("prototype") //控制器一般都要使用多例,单例会影响效率
public class ControllerDemo1 {	
	//当参数名和对象名不一致时
	/*
	 * @RequestParam
	 * 		name:前台给的名字 
	 * 		defaultValue:默认值;name没有值就填默认值
	 * 		required:是否是必填项
	 */
	@RequestMapping(value ="/register3.action",method=RequestMethod.POST)
	public void register3(@RequestParam(name="ue",defaultValue="张三",required=false)String username,@RequestParam(name="pd")String password,@RequestParam(name="hy")String hobby,@RequestParam(name="by")Date birthday,@RequestParam(name="se")double score) {
		System.out.println(username+","+password+","+hobby+","+birthday+","+score);
	}

}

3、jsp

<%@ page language="java" contentType="text/html; charset=Utf-8"
    pageEncoding="Utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<form action="aa/register3.action" method="post">
		用户名:
		<input type="text" name="ue">
		密码:
		<input type="password" name="pd">
		爱好:
		<input type="checkbox" name="hy" value="吃饭">吃饭
		<input type="checkbox" name="hy" value="睡觉">睡觉
		<input type="checkbox" name="hy" value="打豆豆">打豆豆
		出生年月:
		<input type="text" name="by">
		分数:
		<input type="text" name="se">
		<input type="submit" value="提交">
	</form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值