WebTest项目所需包自己下载导入(大致包见最下方列表)
1. 配置文件部分(web.xml,application-context.xml,servlet-mvc.xml)
WebTest\WebContent\WEB-INF\web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
spring监听器
org.springframework.web.context.ContextLoaderListener
Spring 服务层的配置文件
contextConfigLocation
/WEB-INF/classes/xml/application-context.xml
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
spring mvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/classes/xml/servlet-mvc.xml
1
spring mvc
/
10
\WebTest\configuration\xml\application-context.xml
xmlns:p="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-3.1.xsd ">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
WebTest\configuration\xml\servlet-mvc.xml
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="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"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
2.srcjava代码部分
WebTest\src\com\WebTest\test\controller\admin\RegisterController.java
package com.WebTest.test.controller.admin;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.WebTest.test.model.admin.UserModel;
import com.WebTest.test.service.admin.UserService;
import com.WebTest.test.util.CreateConstant;
import com.WebTest.test.util.Page;
@Controller("RegisterController")
@RequestMapping("/register")
public class RegisterController {
@Resource(name = "UserService")
private UserService service = null;
public UserService getService() {
return service;
}
public void setService(UserService service) {
this.service = service;
}
@RequestMapping(value="regis")
public String register(Model model, UserModel user, HttpServletRequest request, HttpServletResponse response) {
try {
user.setId(CreateConstant.getUuIDByTableId());
user.setAge(11);
user.setName("Spring");
service.registerUser(user);
} catch (Exception e) {
e.printStackTrace();
}
return "/one/register";
}
@RequestMapping(value="list")
public String listUser(UserModel user,Model model,HttpServletRequest request, HttpServletResponse response) {
String offset = request.getParameter("pager.offset");
if(offset == null || offset.equals("")) {
offset="0";
}
Page page = service.queryList(Integer.parseInt(offset), Page.SIZE_PER_PAGE);
model.addAttribute("user",user);
model.addAttribute("offset",offset);
model.addAttribute(page);
return "/one/list";
}
}
WebTest\src\com\WebTest\test\dao\admin\IUserDAO.java
package com.WebTest.test.dao.admin;
import java.util.ArrayList;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import com.WebTest.test.model.admin.UserModel;
@Repository("UserDAO")
public interface IUserDAO {
/*@SelectKey(before = false, keyProperty = "user.id", resultType = String.class, statement = { " select last_insert_id() as id; " })*/
@Insert("insert into usermodel(id,name,age,created_time) values(#{user.id},#{user.name},#{user.age},now());")
public void registerUsr(@Param("user") UserModel user);
@Select("select * from usermodel limit #{index},#{size}")
@Results({
@Result(column="created_time",property="createdTime")
})
public ArrayList queryUser(@Param("index") int index, @Param("size") int size);
@Select("select count(*) from usermodel")
public int queryUserCount();
}
WebTest\src\com\WebTest\test\model\admin\UserModel.java
package com.WebTest.test.model.admin;
import java.io.Serializable;
import java.util.Date;
public class UserModel implements Serializable{
private static final long serialVersionUID = 7322263511038551796L;
private String id;
private String name;
private int age;
private Date createdTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
}
WebTest\src\com\WebTest\test\service\admin\UserService.java
package com.WebTest.test.service.admin;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.WebTest.test.dao.admin.IUserDAO;
import com.WebTest.test.model.admin.UserModel;
import com.WebTest.test.util.Page;
@Service("UserService")
public class UserService {
@Resource(name = "UserDAO")
private IUserDAO userDao = null;
public IUserDAO getUserDao() {
return userDao;
}
public void setUserDao(IUserDAO userDao) {
this.userDao = userDao;
}
/**
* 测试新增
*/
public void registerUser(UserModel user) throws Exception{
userDao.registerUsr(user);
}
/**
* 测试查询列表及分页
*/
public Page queryList(int index, int size) {
return new Page(userDao.queryUser(index,size),userDao.queryUserCount());
}
}
WebTest\src\com\WebTest\test\util\CreateConstant.java
package com.WebTest.test.util;
import java.util.UUID;
public class CreateConstant {
public static String getUuIDByTableId() {
String uuid = UUID.randomUUID().toString();
String tableId = uuid.substring(0,8)+uuid.substring(9,13)+uuid.substring(14,18)+
uuid.substring(19,23)+uuid.substring(24);
return tableId;
}
}
3.WebContent页面端部分
WebTest\WebContent\WEB-INF\page\one\list.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager"%>
用户管理
用户管理
cellpadding="0" cellspacing="0" style="margin-top: 10px;">
序号 | id | 姓名 | 年龄 | 新建日期 |
<%
int i = 0;
%>
<%
if (i % 2 == 1) {
i++;
%>
${temp} |
${users.id} |
${users.name} |
${users.age} |
|
<%
} else {
i++;
%>
${temp} |
${users.id} |
${users.name} |
${users.age} |
|
<%
}
%>
style="border: 0; margin: 10px -2px;">
style="clear: both; text-align: center; margin-top: 10px; margin-bottom: 10px;"> 暂无符合查询条件的记录 export="currentPageNumber=pageNumber" maxIndexPages="8" maxPageItems="${page.pageSize}"> |
WebTest\WebContent\WEB-INF\page\one\register.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
out.print("Hellow!Register");
%>
4.下面是大致所需jar包
aopalliance-1.0.jar
asm-3.3.1.jar
aspectjweaver-1.6.10.RC1.jar
bcprov-1.4.jar
cglib-2.2.2.jar
commons-codec-1.3.jar
commons-dbcp-1.4.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
commons-pool-1.5.6.jar
java_memcached-release_2.6.3.jar
jce-1.0.jar
jstl-api-1.2.jar
jstl-impl-1.2.jar
miracle.jar
mybatis-3.0.6.jar
mybatis-spring-1.0.2.jar
mysql-5.1.18-bin.jar
org.springframework.aop-3.1.0.RELEASE.jar
org.springframework.asm-3.1.0.RELEASE.jar
org.springframework.aspects-3.1.0.RELEASE.jar
org.springframework.beans-3.1.0.RELEASE.jar
org.springframework.context-3.1.0.RELEASE.jar
org.springframework.core-3.1.0.RELEASE.jar
org.springframework.expression-3.1.0.RELEASE.jar
org.springframework.jdbc-3.1.0.RELEASE.jar
org.springframework.transaction-3.1.0.RELEASE.jar
org.springframework.web-3.1.0.RELEASE.jar
org.springframework.web.servlet-3.1.0.RELEASE.jar
pager-taglib.jar
pkcs-1.0.jar
poi-3.7-20101029.jar
servlet-api.jar
slf4j-api-1.6.1.jar
slf4j-simple-1.6.1.jar
um-1.0.jar