springParameter--封装User对象、包装类型参数、list集合、map集合、时间转换、重定向和转发、中文乱码解决

package com.converters;
//com.converters 时间转换类
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public  class ConverterTest implements Converter<String, Date>{
public Date convert(String date) {
		if(date.contains("-")) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			try {
				return dateFormat.parse(date);
			}catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		else {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
			try {
				return dateFormat.parse(date);
			}catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}

com.model
package com.model;
//User.java
import java.sql.Date;

public class User {
	
	private int id;
	private String username;
	private String password;
	private Date birthday;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", birthday=" + birthday + "]";
	}
}

package com.model;
//UserCustom.java
import java.util.List;
import java.util.Map;

public class UserCustom{
private User user;
private List<User> userList;
private Map<String,String> maps;

public User getUser() {
	return user;
}

public void setUser(User user) {
	this.user = user;
}


public Map<String,String> getMaps() {
	return maps;
}

public void setMaps(Map<String,String> maps) {
	this.maps = maps;
}

@Override
public String toString() {
	return "UserCustom [user=" + user + ", userlist=" + userList + ", maps=" + maps + "]";
}

public List<User> getUserList() {
	return userList;
}

public void setUserList(List<User> userList) {
	this.userList = userList;
}




}

com.test.controller
package com.test.controller;
//UserController

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.model.User;
import com.model.UserCustom;


@Controller
public class UserController {
	@RequestMapping("toregister")
	public String to() {
		return "index";
	}
	
	//接受参数封装User对象
	@RequestMapping("receiveUser")
	public String receiveUser(User user) {
		System.out.println(user+"22222");
		return "index";
	}

	// 接受包装类型参数
	@RequestMapping("custom")
	public String custom(UserCustom userCustom) {
		System.out.println(userCustom + "3333");
		return "index";
	}
	
	//接受list集合
	@RequestMapping("recieveList")
	public String recieveList(UserCustom userCustom) {
		System.out.println(userCustom);
		return "index";
	}

	//接受map集合
	@RequestMapping("recieveMap")
	public String recieveMap(UserCustom userCustom) {
//		System.out.println(userCustom.getUser());
		System.out.println(userCustom);
		return "index";
	}
	
	@RequestMapping("register")
	public void test(int id,String username,String password,Date birthday) {
	System.out.println(id+","+username+","+password+birthday+","+111111);
}
	//时间转换
	/*public void test(Date birthday) {
		System.out.println(birthday);
	}*/
	
	@RequestMapping("tolist")
	public String testModel(Model m) {
		List<User> list = new ArrayList<User>();
		User u = new User();
		u.setId(1);
		u.setPassword("12345");
		list.add(u);
		User u1 = new User();
		u1.setId(2);
		u1.setPassword("456");		
		list.add(u1);
		m.addAttribute("list",list);
		return "listAll";
	}
	
	/*@RequestMapping("delete")
	public String toEdit(int id,Model m) {
		User u = new User();
		u.setId(id);
		u.setPassword("hello");
		m.addAttribute("user",u);
		return "edit";
	}*/
	

	@RequestMapping("delete")
	public String toEdit(int id,Model m) {
		User u = new User();
		u.setId(id);
		u.setPassword("hello");
		m.addAttribute("user",u);
		return "redirect:toregister.do";//重定向
//		return "forward:toregister.do"; //转发
	}
	
	@RequestMapping("deleteTest/{id}")
	public String torEdit(@PathVariable int id,Model m) {
		User u = new User();
		u.setId(id);
		u.setPassword("hello");
		m.addAttribute("user",u);
		return "edit";
	}
}

springmvc.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  

 http://www.springframework.org/schema/beans/spring-beans.xsd   
 http://www.springframework.org/schema/aop    
 http://www.springframework.org/schema/aop/spring-aop.xsd   
 http://www.springframework.org/schema/jee    
 http://www.springframework.org/schema/jee/spring-jee.xsd   
 http://www.springframework.org/schema/lang    
 http://www.springframework.org/schema/lang/spring-lang.xsd   
 http://www.springframework.org/schema/context    
 http://www.springframework.org/schema/context/spring-context.xsd   
 http://www.springframework.org/schema/tx    
 http://www.springframework.org/schema/tx/spring-tx.xsd   
 http://www.springframework.org/schema/util    
 http://www.springframework.org/schema/util/spring-util.xsd   
 http://www.springframework.org/schema/mvc    
 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 <context:component-scan base-package="com.test.controller"></context:component-scan>
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="prefix" value="/WEB-INF/jsps/"></property>
 <property name="suffix" value=".jsp"></property>
 </bean>
 <!-- 注解驱动 -->
 <mvc:annotation-driven conversion-service="format"></mvc:annotation-driven>
 <!--配置转换器  -->
 <bean  id="format" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
 	<property name="converters">
 	<set>
 	<bean class="com.converters.ConverterTest"></bean>
 	</set>
 	</property>
 </bean>
 </beans>
jsp
//edit.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <!--导入list的标签库  -->
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath}/selectAll.do">
		<input type="text" name="id" value="${user.id}"></br>
		<input type="text" name="password" id="password" value="${user.password}"></br>
		<input type="submit" value="提交">
	</form>
</body>
</html>
//index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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>Insert title here</title>
</head>
<body>
<!-- 接受pojo -->
<form action="${pageContext.request.contextPath}/register.do" method="post">
id<input type="text" name="id" id="id"><br>
姓名:<input type="text" name="username" id="username"><br>
密码:<input type="text" name="password" id="password"><br>	
生日:<input type="text" name="birthday" id="birthday"><br>
<input type="submit" vaue="提交">
</form>

<!--接受包装类型参数  -->
<%-- <form action="${pageContext.request.contextPath}/custom.do" method="post">
id:<input type="text" name="user.id" id="id"><br>
姓名:<input type="text" name="user.username" id="username"><br>
密码:<input type="text" name="user.password" id="password"><br>
	<input type="submit" value="提交">
</form>
 --%>
 
<!--接受集合类型参数  -->
<%-- <form action="${pageContext.request.contextPath}/recieveList.do" method="post">
id:<input type="text" name="userList[0].id" id="id"><br>
姓名:<input type="text" name="userList[0].username" id="username"><br>

id:<input type="text" name="userList[1].id" id="id"><br>
姓名:<input type="text" name="userList[1].username" id="username"><br>
<input type="submit" value="提交">
</form> --%>

<!--接受map  -->
<%-- <form action="${pageContext.request.contextPath}/recieveMap.do" method="post">
id:<input type="text" name="maps['id']" id="id"><br>
姓名:<input type="text" name="maps['username']" id="username"><br>
密码:<input type="text" name="maps['password']" id="password"><br>
<input type="submit" value="提交">
</form> --%>
</body>
</html>
//listAll.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <!--导入list的标签库  -->
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<table border="1">
<tr>
<td>id</td>
<td>密码</td>
</tr>


<!--${list}是一个集合 list是一个循环变量 -->
<!--foreach(int a:arr)  -->
<c:forEach items="${list}" var="list">
<%-- ${list.id}---${list.password}</br> --%>
<tr>
<td> ${list.id}</td>
<td> ${list.password}</td>
<td>
	<a href="${pageContext.request.contextPath}/delete.do?id=${list.id}">修改</a>
	<a href="${pageContext.request.contextPath}/rest/deleteTest/${list.id}">修改Restfull</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
web.xml
//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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springParameter</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
	<filter-name>characterEncoding</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>characterEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<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:springmvc.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>*.do</url-pattern>
	</servlet-mapping>
	
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
</web-app>
jar包:
aopalliance-1.0.jar
aspectjweaver.jar
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
commons-logging-1.2.jar
jstl-1.2.jar
mysql-connector-java-5.1.28-bin.jar
spring-aop-4.1.7.RELEASE.jar
spring-aspects-4.1.7.RELEASE.jar
spring-beans-4.1.7.RELEASE.jar
spring-context-4.1.7.RELEASE.jar
spring-core-4.1.7.RELEASE.jar
spring-expression-4.1.7.RELEASE.jar
spring-jdbc-4.1.7.RELEASE.jar
spring-orm-4.1.7.RELEASE.jar
spring-tx-4.1.7.RELEASE.jar
spring-web-4.1.7.RELEASE.jar
spring-webmvc-4.1.7.RELEASE.jar
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值