springmvc02例


项目结构图:

springmvc02例_第1张图片

UserController.java:

package org.jun.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.jun.dto.User;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *  
 * @author xiejunbo
 * @email [email protected]	
 * @2014年9月29日 下午3:28:57
 **/
/**命名空间根路径http://localhost:8080/springmvc2/user   */
@RequestMapping("/user")//命名空间
@Controller
public class UserController {
	
	/**post请求路径http://localhost:8080/springmvc2/user/addUser.jspx */
	@RequestMapping(value="addUser", method=RequestMethod.POST)
	public String addUser(User user){
		System.out.println(user);
		return "addUser";
	}
	
	/**删除*/
	/*http://localhost:8080/springmvc2/user/deleteUser.jsp*/
	@RequestMapping(value="deleteUser")
	public String deleteUser(User user){
		System.out.println(user);
		return "addUser";
	}
	
	/**修改*/
	/*http://localhost:8080/springmvc2/user/updateUser.jsp*/
	@RequestMapping(value="updateUser")
	public String updateUser(User user){
		System.out.println(user);
		return  "addUser";
	}
	
	/**查询*/
	/*http://localhost:8080/springmvc2/user/addUser.jspx*/
	public String selectUser(User user){
		System.out.println(user);
		return "addUser";
	}
	
	@InitBinder//对WebDataBinder做初始化操作
	public void initBinnner(ServletRequestDataBinder binder){
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-mm-dd"),true));
	}
	
}


User.java:

package org.jun.dto;

import java.util.Date;

/**
 *  dto对象
 * @author xiejunbo
 * @email [email protected]	
 * @2014年9月29日 下午3:35:57
 **/

public class User {

	private String name;
	
	private String sex;
	
	private String school;
	
	private Date birthday;
	
	private String dept;

	public String getName() {
		return name;
	}

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

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getDept() {
		return dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

	@Override
	public String toString() {
		return "User [getName()=" + getName() + ", getSex()=" + getSex()
				+ ", getSchool()=" + getSchool() + ", getBirthday()="
				+ getBirthday() + ", getDept()=" + getDept() + "]";
	}
	
}

 addUser.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>办公管理系统-管理</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="Cache-Control" content="no-cache, must-revalidate"/>
	<meta name="Keywords" content="keyword1,keyword2,keyword3"/>
	<meta name="Description" content="网页信息的描述" />
	<meta name="Author" content="fkjava.org" />
	<meta name="Copyright" content="All Rights Reserved." />
	<link href="fkjava.ico" rel="shortcut icon" type="image/x-icon" />
	<link rel="stylesheet" type="text/css" href="/css/common/commons.css"/>
	<script type="text/javascript" src="/js/common/jquery-1.7.2.js"></script>
	<script type="text/javascript">
	</script>
</head>
<body>
	
	<form action="user/addUser.jspx" method="post">
		姓名:<input type="text" name="name" maxlength="15"/><br/>
		性别:<input type="text" name="sex" maxlength="15"/><br/>
		学校:<input type="text" name="school" maxlength="3"/><br/>
		生日:<input type="text" name="birthday" maxlength="15"/><br/>
		部门:<select name="dept">
			<c:forEach items="${deptList }" var="depts">
				<option value="${depts }">${depts }</option>
			</c:forEach>
		</select>
		<input type="submit" value="添加"/>
	</form>
</body>
</html>

applicationContext-servlet.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:c="http://www.springframework.org/schema/c"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
					    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
					    http://www.springframework.org/schema/context 
					    http://www.springframework.org/schema/context/spring-context-3.1.xsd
					    http://www.springframework.org/schema/mvc 
					    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<!-- 开启annotation注解模式 -->
<mvc:annotation-driven />

<!-- 组件扫描 -->
<context:component-scan base-package="org.jun.controller"/>

<!-- JSP视图解析器 -->
<bean id="excelViewResolver" class="org.springframework.web.servlet.view.InternalResourceView">
		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<property name="suffix" value=".jsp"/>
</bean>

</beans>

applicationContext.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:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
					    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">


</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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
	<!-- 配置springmvc -->
	<servlet>
			<servlet-name>springmvc</servlet-name>
			<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
			<init-param>
					<param-name>contextConfigLocation</param-name>
					<param-value>/WEB-INF/applicationContext*.xml</param-value>
			</init-param>
			<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
			<servlet-name>springmvc</servlet-name>
			<url-pattern>*.jspx</url-pattern>
	</servlet-mapping>

	<!-- 错误页面 -->
	<error-page>
			<error-code>404</error-code>
			<location>/error.jsp</location>
	</error-page>
	<!-- 字符过滤器 -->	
	<filter>
			<filter-name>encodingFilter</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>encodingFilter</filter-name>
			<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- session超时时间 -->
	<session-config>
			<session-timeout>33</session-timeout>
	</session-config>	
	<!-- 加载listener监听器 -->
	<listener>
			<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	   
</web-app>


你可能感兴趣的:(springmvc02例)