开发工具:JDK1.7,Eclipse5.7,Tomcat1.7
框架:spring和struts2框架,还有封装的JDBC
数据库结构:
部门编号:“BM”+流水号(4位) 如BM0001 主键
部门名称:varchar(50)
部门联系方式:varchar(50)
流水号:int(不可以自增长,程序维护)
部门登录口令:varchar(100)MD5 加密
界面:
部门新增
部门编号:只读不可编辑
部门名称:
部门联系方式
流水号:只读不可编辑
部门登录口令
部门列表:
查询:部门名称、部门联系方式(模糊查询)
功能:新增、修改、删除
列表显示:流水号、部门名称、部门联系方式、部门编号
我的项目的目录结构:
报错的部分是极简版的js文件,没有格式,Eclipse无法识别,不影响程序运行。
主要操作com.hymange.web.member包下的类,上面都是封装好的方法不需要动。包下面是日志,JDBC和Struts的一些配置文件,主要操作Struts-web.xml配置文件,其它也不需要动。在WebContent目录下是封装好的页面框架和一些页面样式,也不用动。在WEB-INF目录下,classes是编译文件,jsp目录是放页面的,lib目录下放jar包,web.xml是配置文件,index.jsp是在浏览器中访问程序时进的第一个页面,这里没有写登录,直接把这个页面跳过了。
接下来写配置文件
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_3_0.xsd" id="WebApp_ID" version="3.0">
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>/HY_Mange</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:application*.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<servlet>
<servlet-name>pic</servlet-name>
<servlet-class>com.hymange.common.utils.IdenImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>pic</servlet-name>
<url-pattern>/pic</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>download</servlet-name>
<servlet-class>com.hymange.common.utils.ServletDownload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
因为没写登录,在index.jsp里只是一个跳转
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<s:action name="login" executeResult="true" namespace="/"></s:action>
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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
default-lazy-init="true">
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:bonecp.properties</value>
</property>
</bean>
<!-- datasource -->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="idleConnectionTestPeriodInMinutes" value="${idleConnectionTestPeriodInMinutes}" />
<property name="idleMaxAgeInMinutes" value="${idleMaxAgeInMinutes}" />
<property name="maxConnectionsPerPartition" value="${maxConnectionsPerPartition}" />
<property name="minConnectionsPerPartition" value="${minConnectionsPerPartition}" />
<property name="partitionCount" value="${partitionCount}" />
<property name="acquireIncrement" value="${acquireIncrement}" />
<property name="statementsCacheSize" value="${statementsCacheSize}" />
<property name="releaseHelperThreads" value="${releaseHelperThreads}" />
</bean>
<!-- transaction begin -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- @Transactional <tx:annotation-driven /> -->
<!-- @AspectJ -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- AspectJ AOP ||execution(* com.demo..*(..))-->
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.hymange.*.*.service.*.*(..))" advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- the method which start with get ro find is ready only -->
<tx:method name="get*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="is*" read-only="true" />
<!-- other method is default -->
<tx:method name="*" rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
<!-- transaction end -->
<!-- spring annotation manager -->
<context:annotation-config />
<!-- auto create and wired -->
<context:component-scan base-package="com.hymange.web.*" />
<context:component-scan base-package="com.hymange.common.*" />
</beans>
bonecp.properties配置文件
#æ°æ®åºè¿æ¥é
置信æ¯
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8
#url=jdbc:mysql://192.168.1.88:3306/hymange_test0630?useUnicode=true&characterEncoding=utf-8
#url=jdbc:mysql://127.0.0.1/hymange?useUnicode=true&characterEncoding=utf-8
username=root
password=
#idleConnectionTestPeriod=60
idleConnectionTestPeriodInMinutes=240
#idleMaxAge=240
idleMaxAgeInMinutes=4
maxConnectionsPerPartition=10
minConnectionsPerPartition=5
partitionCount=4
acquireIncrement=5
statementsCacheSize=100
releaseHelperThreads=3
log4j.properties配置文件
log4j.rootLogger=INFO,stdout,file
log4j.addivity.org.apache=true
#log4j.debug=true
# CONSOLE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=INFO
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.Encoding=UTF-8
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
# create log everyday
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=../ccmanager_logs/CCManager
log4j.appender.file.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.file.Encoding=UTF-8
log4j.appender.file.Threshold=INFO
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p %c{1}:%L : %m%n
log4j.logger.com.opensymphony.xwork2=ERROR
struts-default.xml配置文件
<!-- structs 默认配置文件 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/" abstract="true">
<result-types>
<result-type name="jsonResult" class="com.hymange.common.model.JsonResult"></result-type>
<result-type name="excelPoi" class="com.hymange.common.model.ExcelPoiResult"></result-type>
</result-types>
<interceptors>
<interceptor name="actionInterceptor"
class="com.hymange.common.entrance.Interceptor">
</interceptor>
<interceptor name="exceptionInterceptor"
class="com.hymange.common.exception.ExceptionInterceptor">
</interceptor>
<interceptor-stack name="DefaultStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="actionInterceptor"></interceptor-ref>
<interceptor-ref name="exceptionInterceptor"></interceptor-ref>
<interceptor-ref name="token">
<!-- 拦截器参数配置 includeMethods:包含方法,excludeMethods:不包含方法 save,delete,check,reset,invalid -->
<param name="includeMethods"> save,upd</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="DefaultStack"></default-interceptor-ref>
<default-action-ref name="login"></default-action-ref>
<global-results>
<result name="jsonResult" type="jsonResult"></result>
<result name="excelPoi" type="excelPoi">
<param name="inputName">hssfWorkbook</param>
<param name="fileName">fileName</param>
</result>
<result name="invalid.token">***</result>
<result name="exception">/html/404.html</result>
<result name="norole">/html/norole.html</result>
<result name="relogin" type="redirect">login.do</result>
</global-results>
</package>
</struts>
struts.properties配置文件
struts.devMode=false
struts.action.extension=do
struts.enable.DynamicMethodInvocation=true
struts.locale=zh_CN
struts.i18n.encoding=UTF-8
#20M
struts.multipart.maxSize=2147483648
#struts.multipart.saveDir=
struts.i18n.reload=true
struts.ui.theme=simple
struts.objectFactory=spring
struts.serve.static.browserCache=false
struts.url.includeParams=none
struts.custom.i18n.resources=message
#struts.codebehind.pathPrefix=/traincenter/
format.numD0={0,number,\#0.0}
format.numD2={0,number,\#0.00}
struts.xml是Struts主配置文件
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.configuration.xml.reload" value="true"/>
<include file="struts-default.xml" />
<include file="struts-web.xml" />
</struts>
接下来写Java类
1.写一个bean类,映射数据库字段,里面的属性私有,属性名和属性类型要和数据库中的字段一致,并为属性设置get和set方法,代码省略。。。
2.写dao类,在dao类里定义方法写sql语句
package com.hymange.web.member.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.hymange.common.bean.BMember;
import com.hymange.common.exception.DaoException;
import com.hymange.common.model.Clause;
import com.hymange.common.model.Page;
import com.hymange.web.base.dao.BaseDao;
import com.hymange.web.member.bean.DeptBean;
/**
*@Description :部门列表Dao
*@author : Zhaohf
*@since : 2016年7月11日 下午3:38:10
*/
@Repository("MemberDao")
public class MemberDao extends BaseDao {
/**
* 删除部门
*/
public Integer deleteDept(DeptBean bMember,Clause clause){
return this.getSimpleJdbcTemplate().update("delete from dept where d_number='"+bMember.getD_number()+"'");
}
/**
* 新增部门
*/
public Integer insertDept(DeptBean bMember,Clause clause){
return this.getSimpleJdbcTemplate().update("insert into dept (d_number,d_name,d_tel,d_num,d_password) values ("+"'"+bMember.getD_number()+"'"+","+"'"+bMember.getD_name()+"'"+","+"'"+bMember.getD_tel()+"'"+","+"'"+bMember.getD_num()+"'"+","+"'"+bMember.getD_password()+"'"+")");
}
/**
* 部门修改
*/
public Integer updateDept(DeptBean bMember,Clause clause){
// StringBuffer sql = new StringBuffer();
// sql.append("update dept set");
// sql.append(" d_name='"+bMember.getD_name()+"',");
// sql.append(" d_tel='"+bMember.getD_tel()+"',");
// sql.append(" d_password='"+bMember.getD_password()+"' ");
// sql.append("where d_number='"+bMember.getD_number()+"'");
// return this.getSimpleJdbcTemplate().update(sql.toString(), clause,new DeptBean());
return this.getSimpleJdbcTemplate().update("update dept set d_name='"+bMember.getD_name() +"',d_tel='"+bMember.getD_tel()+"',d_password='"+bMember.getD_password()+"' where d_number='"+bMember.getD_number()+"'");
}
/**
* @Description 按条件查询列表
* @throws DaoException
*/
public List<DeptBean> getlistMember(Clause clause, Page page) throws DaoException {
StringBuffer sql = new StringBuffer();
sql.append("SELECT d_number,d_name,d_tel,d_num,d_password FROM dept ");
if(clause.getWhereClause()!=null){
sql.append(" WHERE "+clause.getWhereClause());
}
if(clause.getOrderbyClause()!=null){
sql.append(clause.getOrderbyClause());
}
return this.queryForList(sql, clause.getParams(), page, new DeptBean());
}
/**
* @Description 按条件查询条数
* @return
* @throws DaoException
*/
public Long selectCount(Clause clause) throws DaoException {
StringBuffer sql = new StringBuffer();
sql.append(" SELECT COUNT(*) ");
sql.append(" FROM ");
sql.append(" dept ");
if(clause.getWhereClause()!=null){
sql.append(" WHERE "+clause.getWhereClause());
}
return this.queryForLong(sql.toString(),clause.getParams());
}
/**
* @Description 查询单条数据
* @author Zhaohf
* @param clause
* @return
* @throws DaoException
*/
public DeptBean select (Clause clause) throws DaoException{
StringBuffer sql = new StringBuffer();
sql.append(" SELECT d_number,d_name,d_tel,d_num,d_password ");
sql.append(" FROM ");
sql.append(" dept ");
if(clause.getWhereClause()!=null){
sql.append(" WHERE "+clause.getWhereClause());
}
if(clause.getOrderbyClause()!=null){
sql.append(clause.getOrderbyClause());
}
return this.queryForObject(sql, clause.getParams(), new DeptBean());
}
/**
* @Description 按条件查询条数
* @return
* @throws DaoException
*/
public Long getlistContract(Clause clause) throws DaoException {
StringBuffer sql = new StringBuffer();
sql.append(" SELECT COUNT(*) ");
sql.append(" FROM ");
sql.append(" dept ");
if(clause.getWhereClause()!=null){
sql.append(" WHERE "+clause.getWhereClause());
}
return this.queryForLong(sql.toString(),clause.getParams());
}
}
3.写service类,做逻辑判断,调dao里的方法
package com.hymange.web.member.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.hymange.common.bean.BContract;
import com.hymange.common.bean.BMember;
import com.hymange.common.exception.DaoException;
import com.hymange.common.model.Clause;
import com.hymange.common.model.Constant;
import com.hymange.common.model.Page;
import com.hymange.web.base.service.BaseService;
import com.hymange.web.member.bean.DeptBean;
import com.hymange.web.member.dao.MemberDao;
/**
*@Description : 部门列表Service
*@author : Zhaohf
*@since : 2016年7月11日 下午3:36:58
*/
@Service("MemberService")
public class MemberService extends BaseService {
@Autowired
@Qualifier("MemberDao")
private MemberDao memberDao;
private Clause clause = new Clause();
public List<DeptBean> getlistMember(DeptBean bMember,Page page) throws DaoException{
//设置查询语句
setClause(bMember);
return memberDao.getlistMember(clause,page);
}
public long selectCount(DeptBean bMember) throws DaoException{
//设置查询语句
setClause(bMember);
return memberDao.selectCount(clause);
}
/**
* @Description 设置查询语句
*/
private void setClause(DeptBean bMember){
clause = new Clause();
if(null != bMember.getD_name() && !bMember.getD_name().isEmpty()){
clause.andWhereClause(" d_name LIKE ? ");
clause.addParams(this.reParam(bMember.getD_name()));
}
if(null != bMember.getD_tel() && !bMember.getD_tel().isEmpty()){
clause.andWhereClause(" d_tel LIKE ?" );
clause.addParams(this.reParam(bMember.getD_tel()));
}
}
/**
* 删除部门
*/
public Boolean delete_member(DeptBean bMember) throws DaoException{
boolean flag = false;
if(this.memberDao.deleteDept(bMember, clause)>0){
flag = true;
}
return flag;
}
/**
* 新增部门
*/
public Boolean insert_member(DeptBean bMember) throws DaoException{
boolean flag = false;
// 部门编号和流水号(执行新增操作的方法)
String a = "BM";
String code = this.setLsh("dept", "1", 3);
int d_num=Integer.parseInt(code);
bMember.setD_number(a+code);
bMember.setD_num(d_num);
if(this.memberDao.insertDept(bMember, clause)>0){
flag = true;
}
return flag;
}
//在新增部门跳转时,掉这个方法,计算出部门编号和流水号
public void save_obj(DeptBean bMember) throws DaoException{
// 部门编号和流水号
String a = "BM";
String code = this.setLsh("dept", "1", 3);// dept是表名,1是第一位是1,3是从1后面开始数的三位数
int d_num=Integer.parseInt(code);//把流水号转成int型
bMember.setD_number(a+code);
bMember.setD_num(d_num);
}
/**
* 部门修改
*/
public Boolean update_member(DeptBean bMember) throws DaoException{
boolean flag = false;
if(this.memberDao.updateDept(bMember, clause)>0){
flag = true;
}
return flag;
}
/**
* @Description 查询单条数据
* @author
* @param page
* @return
* @throws DaoException
*/
public DeptBean select(DeptBean bMember) throws DaoException{
clause = new Clause();
clause.andWhereClause(" D_number = ? ");
clause.addParams(bMember.getD_number());
DeptBean bM= this.memberDao.select(clause);
return bM;
}
4.写action类,控制页面跳转和具体操作
package com.hymange.web.member.action;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import com.hymange.common.bean.BContract;
import com.hymange.common.bean.BMember;
import com.hymange.common.exception.DaoException;
import com.hymange.common.model.Constant;
import com.hymange.common.utils.MD5Encrypt;
import com.hymange.web.base.action.BaseAction;
import com.hymange.web.member.bean.DeptBean;
import com.hymange.web.member.service.MemberService;
import sun.misc.BASE64Decoder;
/**
*@Description : 部门列表Action
*@author : Zhaohf
*@since : 2016年7月11日 下午2:28:01
*/
@SuppressWarnings("serial")
@Controller("MemberAction")
public class MemberAction extends BaseAction {
private DeptBean bmember;
private List<DeptBean> bmemberList;
@Autowired
@Qualifier("MemberService")
private MemberService memberService;
/**
* @Description 部门列表跳转
* @author Zhaohf
* @return
*/
public String member_list(){
bmember = new DeptBean();
try {
bmemberList = memberService.getlistMember(bmember,this.getPage());
long count = memberService.selectCount(bmember);
this.toJsonData(bmemberList, count);
} catch (DaoException e) {
e.printStackTrace();
}
return"member_list";
}
/**
* @Description 部门列表查询
* @author
* @return
*/
public String select_member_list(){
if(null == bmember)bmember = new DeptBean();
String d_name = ServletActionContext.getRequest().getParameter("d_name");
String d_tel = ServletActionContext.getRequest().getParameter("d_tel");
bmember.setD_number(d_name);
bmember.setD_number(d_tel);
try {
bmemberList = memberService.getlistMember(bmember,this.getPage());
long count = memberService.selectCount(bmember);
this.toJsonData(bmemberList, count);
} catch (DaoException e) {
e.printStackTrace();
}
return "jsonResult";
}
/**
* @Description 新增部门页跳转
* @author
* @return
*/
public String member_add(){
bmember = new DeptBean();
try {
//掉计算流水号的方法,然后把得到的值传到页面显示出来
this.memberService.save_obj(bmember);
ServletActionContext.getRequest().setAttribute("bmember", bmember);
} catch (DaoException e) {
e.printStackTrace();
}
this.setHandle("ADD");
return"member_add";
}
/**
* 新增部门
*/
public String save_member(){
//MD5加密
bmember.setD_password(MD5Encrypt.MD5Encode(bmember.getD_password().substring(bmember.getD_password().length())));
try {
if(memberService.insert_member(bmember)==true){
this.toJsonMsg("success", "保存成功");
} else {
this.toJsonMsg("error", "系统繁忙。。。");
}
} catch (DaoException e) {
e.printStackTrace();
this.toJsonMsg("error", "系统繁忙。。。");
}
return Constant.JSON_RESULT;
}
/**
* @Description 部门修改页跳转
* @author
* @return
*/
public String member_edit(){
try {
bmember = this.memberService.select(bmember);
ServletActionContext.getRequest().setAttribute("bmember", bmember);
} catch (DaoException e) {
e.printStackTrace();
}
this.setHandle("EDIT");
return"member_edit";
}
/**
* 部门修改
*/
public String edit_member(){
//MD5加密
bmember.setD_password(MD5Encrypt.MD5Encode(bmember.getD_password().substring(bmember.getD_password().length())));
try {
if(memberService.update_member(bmember)==true){
this.toJsonMsg("success", "保存成功");
} else {
this.toJsonMsg("error", "系统繁忙。。。");
}
} catch (DaoException e) {
e.printStackTrace();
this.toJsonMsg("error", "系统繁忙。。。");
}
return Constant.JSON_RESULT;
}
/**
* 删除部门
*/
public String delete_member(){
try {
if(memberService.delete_member(bmember)==true){
this.toJsonMsg("success", "删除成功");
} else {
this.toJsonMsg("error", "系统繁忙。。。");
}
} catch (DaoException e) {
e.printStackTrace();
this.toJsonMsg("error", "系统繁忙。。。");
}
return Constant.JSON_RESULT;
}
//get和set方法
public List<DeptBean> getBmemberList() {
return bmemberList;
}
public void setBmemberList(List<DeptBean> bmemberList) {
this.bmemberList = bmemberList;
}
public DeptBean getBmember() {
return bmember;
}
public void setBmember(DeptBean bmember) {
this.bmember = bmember;
}
5.写Struts-web.xml配置文件,他负责链接action类和页面的
<!-- web structs配置文件 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="web" extends="default" namespace="/">
<!-- 部门列表-->
<action name="member" class="MemberAction">
<result name="member_list">/WEB-INF/jsp/member/member_list.jsp</result>
<result name="member_add">/WEB-INF/jsp/member/member_add.jsp</result>
<result name="look_info">/WEB-INF/jsp/member/member_detail.jsp</result>
<result name="member_testpassword_page">/WEB-INF/jsp/member/member_testPassword.jsp</result>
<result name="member_edit">/WEB-INF/jsp/member/member_edit.jsp</result>
</action>
</package>
</struts>
namespace="/" 空间名,/是没有,如果写了,在浏览器中访问时把链接打上
action里的 name="member" class="MemberAction" name是起的名,class是交给它管理的类
result里的 name="member_list" name是交给它管理的类里的方法名,标签体里是他要跳转的jsp页面
6.写jsp页面
部门列表页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>部门列表</title>
<%@include file="../common/base.jsp"%>
<link rel="stylesheet" type="text/css" href="frame_other/datepicker/jquery.datetimepicker.css"/>
<!-- <script src="js/area.js" type="text/javascript"></script> -->
<script type="text/javascript" src="frame_other/datepicker/jquery.datetimepicker.js"></script>
</head>
<body>
<div style="width:100%; height:40px; background: #2e8a65; text-align: left;line-height: 40px;color: #FFF;padding-left: 10px; ">当前位置:部门列表</div>
<div class="manger_main">
<div class="manger_message">
<div class="manger_message_left">
<img src="images/icgss.png" />操作提示:
<span>查看部门列表</span>
</div>
</div>
<form id="queryform" class="manger_query" method="post">
<dl>
<dt>部门名称 </dt>
<dd>
<input type="text" name="bmember.d_name" >
</dd>
<dt>部门联系方式 </dt>
<dd>
<input type="text" name="bmember.d_tel" >
</dd>
</dl>
<dl>
<dt>
<a class="btnorange chax" href="javascript:selectList();">查询</a>
</dt>
</dl>
</form>
<div class="add_left">
<a class="btnorange bianj" href="javascript:add();">新增部门</a>
<a class="btnorange bianj" href="javascript:edit();">修改部门</a>
<a href="javascript:testPWD();" class="bianjis orange">删除</a>
</div>
<div class="manger_content">
<form action="member!member_edit.do" id= "edit">
<table id="datagrid" class="easyui-datagrid"
style="width: 100%; height: 425px;"
data-options="pagination:true,singleSelect:true">
<thead>
<tr>
<th data-options="field:'ck',checkbox:true,width:'2%'"></th>
<th data-options="field:'d_number',width:'15%'">部门编号</th>
<th data-options="field:'d_name',width:'10%'">部门名称</th>
<th data-options="field:'d_num',width:'15%'">流水号</th>
<th data-options="field:'d_tel',width:'15%'">部门联系方式</th>
</tr>
</thead>
</table>
</form>
</div>
</div>
<script type="text/javascript">
<!--_init_area();-->
$(function() {
//datagrid初始化加载数据,配置相关参数
var jsonResult = ${jsonResult};
if (jsonResult.msgFlag == "1") {
$("#datagrid").datagrid('loadData', jsonResult);
$("#datagrid").datagrid('options').url = 'member!select_member_list.do';
}
})
// 查看
function look_info(){
}
//查询
function selectList() {
easyuiParam();
$("#datagrid").datagrid('load');
}
// 新增
function add() {
window.location.href='member!member_add.do';
}
// 修改选中数据
function edit(){
var row = $('#datagrid').datagrid('getSelected');
if(row==null){
alert("请选择要修改的部门");
return ;
}
window.location.href= "member!member_edit.do?bmember.d_number="+ row.d_number;
}
// 删除
function testPWD(){
var row = $('#datagrid').datagrid('getSelected');
if(row==null){
alert("请选择要删除的部门");
return ;
}
if(confirm("您确认删除吗?")){
if($("#edit").valid()){
var url = "member!delete_member.do?bmember.d_number="+ row.d_number;
$.post(url, $("#edit").serializeArray(),
function(result) {
if (result.msgFlag=="success"){
alert(result.msgContent);
window.location.reload();//刷新页面
} else {
alert(result.msgContent);
}
},"json");
}
}
}
</script>
</body>
</html>
部门新增页面
<!DOCTYPE HTML>
<%@page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>部门新增</title>
<%@include file="/WEB-INF/jsp/common/base.jsp"%>
<link rel="stylesheet" type="text/css" href="frame_other/datepicker/jquery.datetimepicker.css"/>
<script type="text/javascript" src="frame_other/datepicker/jquery.datetimepicker.js"></script>
<script src="js/area.js" type="text/javascript"></script>
<script src="js/area2.js" type="text/javascript"></script>
</head>
<body>
<div style="width:100%; height:40px; background: #2e8a65; text-align: left;line-height: 40px;color: #FFF;padding-left: 10px; ">当前位置:部门列表/<s:if test="handle=='ADD'">部门新增</s:if><s:else>会员编辑</s:else></div>
<div class="manger_main">
<div class="add_left">
<a id='form_submit' href="javascript:form_submit();" class="btnorange baoc">保存</a>
<s:if test="handle=='EDIT'">
<a id='member_list' href="member!member_list.do" class="btnorange fanh">返回列表</a>
</s:if>
</div>
<div id="content" class="manger_content">
<form id="editform" action="" method="post" class="edit_form">
<input id="path" name="bmember.m_idcard_img1" type="hidden" value="" style="display: none"/>
<input id="path1" name="bmember.m_idcard_img2" type="hidden" value="" style="display: none"/>
<ul>
<div class="set_name"><span>*</span>部门编号:</div>
<div class="set_input"><input id="d_number" name="bmember.d_number" readonly="readonly" value="${bmember.d_number}" type="text" class="{required:true}" /></div>
<p class="error_tit"></p>
</ul>
<ul>
<div class="set_name"><span>*</span>部门名称:</div>
<div class="set_input"><input id="d_name" name="bmember.d_name" value="${bmember.d_name}" type="text" class="{required:true,maxlength:100}"/></div>
<p class="error_tit"></p>
</ul>
<ul>
<div class="set_name"><span>*</span>联系方式:</div>
<div class="set_input"><input id="d_tel" name="bmember.d_tel" value="${bmember.d_tel }" type="text" class="{required:true,number:true}" maxlength="20"/></div>
<p class="error_tit"></p>
</ul>
<ul>
<div class="set_name"><span>*</span>流水号:</div>
<div class="set_input"><input id="d_num" name="bmember.d_num" readonly="readonly" value="${bmember.d_num}" type="text" class="{required:true}" maxlength="20"/></div>
<p class="error_tit"></p>
</ul>
<ul>
<div class="set_name"><span>*</span>登陆口令:</div>
<div class="set_input"><input id="d_password" name="bmember.d_password" value="${bmember.d_password}" type="text" class="{required:true,maxlength:100}" maxlength="50"/></div>
<p class="error_tit"></p>
</ul>
<br>
</form>
</div>
<div class="add_left">
<a id='form_submit' href="javascript:form_submit();" class="btnorange baoc">保存</a>
<s:if test="handle=='ADD'">
<a id='member_list' href="member!member_list.do" class="btnorange fanh">返回列表</a>
</s:if>
</div>
</div>
<script type="text/javascript">
$(function(){
//绑定表单验证
editformValidate();
})
function form_submit(){
var url = "member!save_member.do";
//验证表单
if($("#editform").valid()){
//验证电话号码
var phone = /^0?1[3|4|5|7|8][0-9]\d{8}$/;
if(!phone.test($('#d_tel').val())){
alert("电话号码不符合规则,请重新输入");
return;
}
$.ajax({
url:url,
data: {"bmember.d_number":$("#d_number").val(),
"bmember.d_name":$("#d_name").val(),
"bmember.d_tel":$("#d_tel").val(),
"bmember.d_num":$("#d_num").val(),
"bmember.d_password":$("#d_password").val()},
type:"post",
dataType:"json",
success: function(result){
if (result.msgFlag=="success"){
alert(result.msgContent);
$("#form_submit").attr("href","javascript:doublesubmit();");
}else {
alert(result.msgContent);
}
}
});
//alert("---已经保存成功!!!");
}
function doublesubmit(){
parent.alertmessage("已经保存成功!!!");
return;
}
}
</script>
</body>
</html>
修改部门页面
<!DOCTYPE HTML>
<%@page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>修改部门</title>
<%@include file="/WEB-INF/jsp/common/base.jsp"%>
<link rel="stylesheet" type="text/css" href="frame_other/datepicker/jquery.datetimepicker.css"/>
<script type="text/javascript" src="frame_other/datepicker/jquery.datetimepicker.js"></script>
<script src="js/area.js" type="text/javascript"></script>
<script src="js/area2.js" type="text/javascript"></script>
</head>
<body>
<div style="width:100%; height:40px; background: #2e8a65; text-align: left;line-height: 40px;color: #FFF;padding-left: 10px; ">当前位置:部门列表/<s:if test="handle=='EDIT'">修改部门</s:if><s:else>修改部门</s:else></div>
<div class="manger_main">
<div class="add_left">
<a id='form_submit' href="javascript:form_submit();" class="btnorange baoc">保存</a>
<s:if test="handle=='ADD'">
<a id='member_list' href="member!member_list.do" class="btnorange fanh">返回列表</a>
</s:if>
</div>
<div id="content" class="manger_content">
<form id="editform" action="" method="post" class="edit_form">
<input id="path" name="bmember.m_idcard_img1" type="hidden" value="" style="display: none"/>
<input id="path1" name="bmember.m_idcard_img2" type="hidden" value="" style="display: none"/>
<ul>
<div class="set_name"><span>*</span>部门编号:</div>
<%-- <s:if test="handle=='ADD'"> --%>
<div class="set_input"><input id="d_number" name="bmember.d_number" readonly="readonly" value="${bmember.d_number}" type="text" class="{required:true}" /></div>
<p class="error_tit"></p>
</ul>
<ul>
<div class="set_name"><span>*</span>部门名称:</div>
<div class="set_input"><input id="d_name" name="bmember.d_name" value="${bmember.d_name}" type="text" class="{required:true,maxlength:100}"/></div>
<p class="error_tit"></p>
</ul>
<ul>
<div class="set_name"><span>*</span>联系方式:</div>
<div class="set_input"><input id="d_tel" name="bmember.d_tel" value="${bmember.d_tel }" type="text" class="{required:true,number:true}" maxlength="20"/></div>
<p class="error_tit"></p>
</ul>
<ul>
<div class="set_name"><span>*</span>流水号:</div>
<div class="set_input"><input id="d_num" name="bmember.d_num" value="${bmember.d_num}" readonly="readonly" type="text" class="{required:true}" maxlength="20"/></div>
<p class="error_tit"></p>
</ul>
<ul>
<div class="set_name"><span>*</span>登陆口令:</div>
<div class="set_input"><input id="d_password" name="bmember.d_password" value="${bmember.d_password}" type="text" class="{required:true,maxlength:100}" maxlength="50"/></div>
<p class="error_tit"></p>
</ul>
<br>
</form>
</div>
<div class="add_left">
<a id='form_submit' href="javascript:form_submit(); " class="btnorange baoc">保存</a>
<s:if test="handle=='EDIT'">
<a id='member_list' href="member!member_list.do" class="btnorange fanh">返回列表</a>
</s:if>
</div>
</div>
<script type="text/javascript">
$(function(){
//绑定表单验证
editformValidate();
})
function form_submit(){
var url = "member!edit_member.do";
//验证表单
if($("#editform").valid()){
//验证电话号码
var phone = /^0?1[3|4|5|7|8][0-9]\d{8}$/;
if(!phone.test($('#d_tel').val())){
alert("电话号码不符合规则,请重新输入");
return;
}
$.ajax({
url:url,
data: {"bmember.d_number":$("#d_number").val(),
"bmember.d_name":$("#d_name").val(),
"bmember.d_tel":$("#d_tel").val(),
"bmember.d_num":$("#d_num").val(),
"bmember.d_password":$("#d_password").val()},
type:"post",
dataType:"json",
success: function(result){
if (result.msgFlag=="success"){
alert(result.msgContent);
$("#form_submit").attr("href","javascript:doublesubmit();");
}else {
alert(result.msgContent);
}
}
});
//alert("---已经修改成功!!!");
}
}
function doublesubmit(){
parent.alertmessage("已经保存成功!!!");
return;
}
/* 验证电话号码
var phone = /^0?1[3|4|5|7|8][0-9]\d{8}$/;
if(!phone.test($('#d_tel').val())){
parent.alertmessage("电话号码不符合规则,请重新输入");
return;
}
*/
</script>
</body>
</html>
效果展示:
部门列表页面
部门新增页面
修改部门页面
删除效果
查询效果