WebTest项目所需包自己下载导入(大致包见最下方列表)
1. 配置文件部分(web.xml,application-context.xml,servlet-mvc.xml)
WebTest\WebContent\WEB-INF\web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
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">
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<description>Spring 服务层的配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/xml/application-context.xml
</param-value>
</context-param>
<!-- Spring MVC 字符编码过滤器 -->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/xml/servlet-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
</web-app>
\WebTest\configuration\xml\application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<p:beans
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 ">
<p:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<p:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<p:property name="username" value="root" />
<p:property name="password" value="root" />
<p:property name="url" value="jdbc:mysql://10.128.179.11:3306/tpay" />
<p:property name="minIdle" value="10" />
</p:bean>
<p:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<p:property name="dataSource" ref="dataSource" />
</p:bean>
<p:bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<p:property name="dataSource" ref="dataSource" />
</p:bean>
<p:bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<p:property name="sqlSessionFactory" ref="sqlSessionFactory" />
<p:property name="basePackage" value="com.WebTest.test.dao.*" />
</p:bean>
</p:beans>
WebTest\configuration\xml\servlet-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<p:beans
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 ">
<!-- 启动Spring注解功能 -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 自动扫描bean,把作了注解的类转换为bean -->
<context:component-scan base-package="com.WebTest.*">
<context:exclude-filter type="regex" expression="com.WebTest.test.dao.*" />
</context:component-scan>
<!-- 映射视图 -->
<p:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<p:property name="prefix" value="/WEB-INF/page/" />
<p:property name="suffix" value=".jsp" />
</p:bean>
<!-- <mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/admin/**" />
<p:bean class="com.beyondsoft.base.filter.ManagerInterceptor" />
</mvc:interceptor>
商户登录拦截器
<mvc:interceptor>
<mvc:mapping path="/merchant/**" />
<p:bean class="com.beyondsoft.base.filter.MerchantInterceptor" />
</mvc:interceptor>
</mvc:interceptors> -->
</p:beans>
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<UserModel> 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"%>
<!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>用户管理</title>
<script src="${pageContext.request.contextPath}/js/jquery.js"></script>
<script src="${pageContext.request.contextPath}/js/changecolor.js"></script>
<script language="javascript">
$(document).ready(function(){
//列表颜色随鼠标而变化
$.change();
});
</script>
<style type="text/css">
<!-- 这些css是用来做分页按钮的样式,当然可改了 -->
a:link,a:visited{
color:#025ea7;
text-decoration:none;
}
a:hover,a:active{
color:#0669AC;
text-decoration:none;
}
a.link:link,a.link:visited{ color:#5e5e5e;}
.red{ color:#ff6600; font-weight:normal;}
.legend{
margin-left:10px;
color: #000000;
font-weight:bold;
color:#575757;
line-height:24px;
}
.page{float:right; height:26px;}
.page ul li{float:left;min-width:18px; height:18px; border:#e9e9e9 solid 1px; line-height:18px; text-align:center; margin-left:6px;cursor:pointer;}
.page ul li.arrow{ background:#efefef; line-height:18px; font-weight:bold;color:#666666;}
.page ul li.on{background:#e9490a; color:#fff;}
.page ul li.on a:link{color:#fff; text-decoration:none;}
.page ul li.on a:visited{color:#fff;}
.page ul a:hover{color:#ff6600;}
.gotopage_input{width:30px;}
.goto_btn{
border:#2b2b2b solid 1px;
height:18px;
line-height:18px;
background:#cfcfcf;
cursor: pointer;
font-size:12px;
color:#000;
font-family:Arial;
}
span.red{ color:#ff6600;font-weight:normal;}
.btn_area{ url(${pageContext.request.contextPath} /p_w_picpaths/btn_area_bg.gif) repeat-x; height:34px;margin:6px; line-height:34px;padding-left:6px;}
</style>
<title>用户管理</title>
</head>
<body>
<div>
<table width="100%" id="table" class="fieldset" border="0"
cellpadding="0" cellspacing="0" style="margin-top: 10px;">
<tr>
<td>序号</th>
<td>id</th>
<td>姓名</th>
<td>年龄</th>
<td>新建日期</th>
</tr>
<c:if test="${page.data.size() != 0 }">
<c:set var="temp" value="${offset+1}"></c:set>
<%
int i = 0;
%>
<c:forEach items="${page.data}" var="users">
<%
if (i % 2 == 1) {
i++;
%>
<tr>
<td height="29" align="center">${temp}</td>
<c:set var="temp" value="${temp+1}"></c:set>
<td align="left">${users.id}</td>
<td align="left">${users.name}</td>
<td align="left">${users.age}</td>
<td align="left">
<fmt:formatDate value="${users.createdTime}" pattern="yyyy-MM-dd"/>
</td>
</tr>
<%
} else {
i++;
%>
<tr class="on">
<td height="29" align="center">${temp}</td>
<c:set var="temp" value="${temp+1}"></c:set>
<td align="left">${users.id}</td>
<td align="left">${users.name}</td>
<td align="left">${users.age}</td>
<td align="left">
<fmt:formatDate value="${users.createdTime}" pattern="yyyy-MM-dd"/>
</td>
</tr>
<%
}
%>
</c:forEach>
</c:if>
</table>
<!--分页-->
<table width="100%" border="0" cellspacing="0" cellpadding="0"
style="border: 0; margin: 10px -2px;">
<tr>
<td width="81%" style="border: 0;">
<div
style="clear: both; text-align: center; margin-top: 10px; margin-bottom: 10px;">
<c:choose>
<c:when test="${empty page }">
</c:when>
<c:otherwise>
<c:if test="${page.totalCount < 1}">
<span align="center" style="color: red;">暂无符合查询条件的记录</span>
</c:if>
<c:if test="${page.totalCount >= 1}">
<pg:pager items="${page.totalCount}" url="list"
export="currentPageNumber=pageNumber" maxIndexPages="8"
maxPageItems="${page.pageSize}">
<pg:param name="name" value="${user.name}" />
<div class="page">
<ul>
<c:if test="${page.totalCount!=0}">
<pg:first>
<li><a href="${pageUrl}">首页</a></li>
</pg:first>
<pg:prev>
<li><a href="${pageUrl}">前一页</a></li>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber}">
<li class="on">${pageNumber}</li>
</c:when>
<c:otherwise>
<a href="${pageUrl}"><li>${pageNumber}</li></a>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<li><a href="${pageUrl}">后一页</a></li>
</pg:next>
<pg:last>
<li><a href="${pageUrl}">尾页</a></li>
</pg:last>
</c:if>
<c:if test="${page.totalCount==0}">
<li>首页</li>
<li>尾页</li>
</c:if>
</ul>
<div class="clear"></div>
</div>
<div class="clear"></div>
</pg:pager>
</c:if>
<!--翻页 end-->
</c:otherwise>
</c:choose>
</div>
</td>
</tr>
</table>
</div>
</div>
</div>
</body>
</html>
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
转载于:https://blog.51cto.com/3131854/839763