springmvc学习笔记(七)数据传递及乱码问题

先做个例子:在页面输入数据传递到Controller处理,再返回到页面显示数据.

1.Controller类

@Controller
@RequestMapping("/user")
public class UserController {
	@RequestMapping("/addUser")
	public String addUser(String name, String age, HttpServletRequest request){
		request.setAttribute("result", "addUser-----");
		System.out.println("name=" + name);
		request.setAttribute("result", name);
		return "annotation";
	}
	@RequestMapping("/delUser")
	public String delUser(HttpServletRequest request){
		request.setAttribute("result", "delUser-----");
		return "annotation";
	}
	
	@RequestMapping("/toUser")
	public String toUser(HttpServletRequest request){
		request.setAttribute("result", "toUser-----");
		return "adduserView";
	}
}

2.adduserView.jsp 输入数据页面.

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   

  
    My JSP 'welcom.jsp' starting page
  
  
	
姓名:
年龄:

这里先以get方式请求吧,把document.form[0].method="post"改为document.form[0].method="get"

3.显示数据页面 annotatio.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   

  
    My JSP 'welcom.jsp' starting page
    
  
  
  
	
spring mvc学习笔记之注解开启

${result }

4.部署测试,输入中文字符串,页面显示乱码

get方式传递参数造成的乱码可这样修改:

Tomcat下的server.xml

    

添加了一个URIEncoding="UTF-8"


5.Post方式传递数据乱码解决.

	@RequestMapping("/addUser")
	public String addUser(String name, String age, HttpServletRequest request){
		request.setAttribute("result", "addUser-----");
		try {
			name = new String(name.getBytes("iso-8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
		}
		System.out.println("name=" + name);
		request.setAttribute("result", name);
		return "annotation";
	}

这样解决未免太过复杂.

还有一种方式,通过在web.xml添加一个过滤器,对所有传递的数据进行一次编码转换

    
    Character Encoding    
    org.springframework.web.filter.CharacterEncodingFilter    
        
        encoding    
        UTF-8    
        
    
    
    Character Encoding    
    /*    
  

这是spirngmvc自带的过滤器.

又在网上看到更好的方式解决这个问题,如果还在web.xml中加入过滤器,那请求就被拦截了两次,不太好,可以合并成一个拦截.

继承DispactcherServlet,修改编码方式:

package com.skymr.mvc;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.DispatcherServlet;

public class EncodingDispatcherServlet extends DispatcherServlet{

	protected String encoding;
	
	@Override
	protected void doService(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.setCharacterEncoding(encoding);
		super.doService(request, response);
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		encoding = config.getInitParameter("encoding");
		super.init(config);
	}

}

将web.xml中的springmvc的servlet改这个servlet,并设置参数encoding 为utf-8

      
        springMVC  
        com.skymr.mvc.EncodingDispatcherServlet  
        
        	contextConfigLocation
        	classpath*:/config/spring_annotation-servlet.xml
        
        
        	encoding
        	UTF-8
        
        1  
      
      
        springMVC  
        /  
      

6.传递实体数据

实体类

package com.skymr.mvc.entity;

public class User {

	private String name;
	
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	
}


Controller类修改

	@RequestMapping("/addUser")
	public String addUser(User user, HttpServletRequest request){
		request.setAttribute("result", "addUser-----");
		System.out.println("name=" + user.getName());
		request.setAttribute("user", user);
		return "annotation";
	}
显示

	
spring mvc学习笔记之数据传递

${user.name },${user.age }






你可能感兴趣的:(springmvc)