MaTouChuanZhi Project construction
Step One. Organize the required jar package resources
- Spring frame jar package: spring.jar
- Hibernate fram jar package: hibernate3.jar,hibernate-jpa-2.0-api-1.0.0.Final.jar
- struts2 fram jar package:struts2-core-2.2.1.jar, struts2-spring-plugin-2.2.1.jar
- The jar package connected to the database (using mysql as an example):mysql-connector-java-5.1.5-bin.jar
- The jar package connected to the database (using mysql as an example):mysql-connector-java-5.1.5-bin.jar
- Pictures of the jar package required for the project:
Step Two. Project construction
- Create a new Dynamic Web Project,Dynamic web moudule version choose 2.5
- Import jar package
- Configuring 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_2_5.xsd" id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>shouye.jsp</welcome-file>
</welcome-file-list>
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring的监听器配置开始 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>lazy</filter-name>
<filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>lazy</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts2的过滤器 -->
<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>
</web-app>
- Create project architecture packages:
action,business,dao,model
Four core packages
– Writing entity classes
package com.allde.wo.chuanzhi.model;
import com.allde.framework.model.AbstractModel;
public class ChuanzhiModel extends AbstractModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String num;//船号
private String name;//船名
private String tiji;//船只体积
private String yongtu;//船只用途
private String xiangxi;//详细信息
private String beizhu;//备注
private String ttime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTiji() {
return tiji;
}
public void setTiji(String tiji) {
this.tiji = tiji;
}
public String getYongtu() {
return yongtu;
}
public void setYongtu(String yongtu) {
this.yongtu = yongtu;
}
public String getXiangxi() {
return xiangxi;
}
public void setXiangxi(String xiangxi) {
this.xiangxi = xiangxi;
}
public String getBeizhu() {
return beizhu;
}
public void setBeizhu(String beizhu) {
this.beizhu = beizhu;
}
public String getTtime() {
return ttime;
}
public void setTtime(String ttime) {
this.ttime = ttime;
}
@Override
public String toString() {
return "ChuanzhiModel [id=" + id + ", num=" + num + ", name=" + name
+ ", tiji=" + tiji + ", yongtu=" + yongtu + ", xiangxi="
+ xiangxi + ", beizhu=" + beizhu + ", ttime=" + ttime + "]";
}
}
– Writing ChuanzhiModel.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.allde.wo.chuanzhi.model.ChuanzhiModel">
<id name="id">
<column name="id" />
<generator class="native" />
</id>
<property name="num" />
<property name="name" />
<property name="tiji" />
<property name="yongtu" />
<property name="xiangxi" />
<property name="beizhu" />
<property name="ttime" />
</class>
</hibernate-mapping>
– Writing interface ChuanzhiDao.java
package com.allde.wo.chuanzhi.dao.dao;
import com.allde.framework.dao.dao.DAO;
import com.allde.wo.chuanzhi.model.ChuanzhiModel;
import com.allde.wo.chuanzhi.model.ChuanzhiQueryModel;
public interface ChuanzhiDao extends DAO<ChuanzhiModel, ChuanzhiQueryModel> {
}
– Writing interface ChuanzhiEbi.java
package com.allde.wo.chuanzhi.business.ebi;
import com.allde.framework.business.ebi.Ebi;
import com.allde.wo.chuanzhi.model.ChuanzhiModel;
import com.allde.wo.chuanzhi.model.ChuanzhiQueryModel;
public interface ChuanzhiEbi extends Ebi<ChuanzhiModel, ChuanzhiQueryModel> {
}
– Writing class ChuanzhiEbo.java
package com.allde.wo.chuanzhi.business.ebo;
import com.allde.framework.business.ebo.AbstractEbo;
import com.allde.framework.dao.dao.DAO;
import com.allde.wo.chuanzhi.business.ebi.ChuanzhiEbi;
import com.allde.wo.chuanzhi.dao.dao.ChuanzhiDao;
import com.allde.wo.chuanzhi.model.ChuanzhiModel;
import com.allde.wo.chuanzhi.model.ChuanzhiQueryModel;
public class ChuanzhiEbo extends AbstractEbo<ChuanzhiModel, ChuanzhiQueryModel> implements
ChuanzhiEbi {
private ChuanzhiDao chuanzhiDao;
public void setChuanzhiDao(ChuanzhiDao chuanzhiDao) {
this.chuanzhiDao = chuanzhiDao;
}
@Override
protected DAO<ChuanzhiModel, ChuanzhiQueryModel> getDao() {
// TODO Auto-generated method stub
return chuanzhiDao;
}
}
– Writing class ChuanzhiAction.java
package com.allde.wo.chuanzhi.action;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.allde.wo.chuanzhi.business.ebi.ChuanzhiEbi;
import com.allde.wo.chuanzhi.model.ChuanzhiModel;
import com.allde.wo.chuanzhi.model.ChuanzhiQueryModel;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class ChuanzhiAction extends ActionSupport implements ModelDriven<ChuanzhiModel> {
private ChuanzhiModel chuanzhiModel = new ChuanzhiModel();
private ChuanzhiEbi chuanzhiEbi;
public void setChuanzhiEbi(ChuanzhiEbi chuanzhiEbi) {
this.chuanzhiEbi = chuanzhiEbi;
}
public String create() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String ttime = df.format(new Date());// new Date()为获取当前系统时间
chuanzhiModel.setTtime(ttime);
chuanzhiEbi.create(chuanzhiModel);
return SUCCESS;
}
//更具id得到用户详细信息
public String getUserid(){
HttpServletRequest request = ServletActionContext.getRequest();
int id = Integer.parseInt(request.getParameter("id"));
ChuanzhiQueryModel qm = new ChuanzhiQueryModel();
qm.setId(id);
request.setAttribute("newsxx", chuanzhiEbi.getByCondition(qm, 0, 20));
return "xxid";
}
//根据Id得到用户信息去修改
public String getUserById(){
HttpServletRequest request = ServletActionContext.getRequest();
int id = Integer.parseInt(request.getParameter("id"));
ChuanzhiQueryModel qm = new ChuanzhiQueryModel();
qm.setId(id);
request.setAttribute("newsupdate", chuanzhiEbi.getByCondition(qm, 0, 20));
return "byid";
}
//删除
public String deletedById() {
HttpServletRequest request = ServletActionContext.getRequest();
int id = Integer.parseInt(request.getParameter("id"));
chuanzhiEbi.delete(id);
return "delete";
}
//得到所有
public String getAll() {
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("newsmodel",chuanzhiEbi.getAll(0, 6));
request.setAttribute("newsnum",chuanzhiEbi.getAllCount());
return "newsall";
}
//得到所有看
public String getAllkan() {
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("newsmodel",chuanzhiEbi.getAll(0, 6));
request.setAttribute("newsnum",chuanzhiEbi.getAllCount());
return "newsallkan";
}
public ChuanzhiModel getModel() {
return chuanzhiModel;
}
public String update() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String ttime = df.format(new Date());// new Date()为获取当前系统时间
chuanzhiModel.setTtime(ttime);
chuanzhiEbi.update(chuanzhiModel);
return "update";
}
//分页显示首页
public String getAllFen(){
int j = chuanzhiEbi.getAllCount();
int jj=j-j%6;
HttpServletRequest request = ServletActionContext.getRequest();
int i = Integer.parseInt(request.getParameter("i"));
if (i >=j) {
request.setAttribute("i", jj);
request.setAttribute("newsmodel", chuanzhiEbi.getAll(jj, 6));
}else if(i<=0){
request.setAttribute("newsmodel", chuanzhiEbi.getAll(0, 6));
} else {
request.setAttribute("i", i);
request.setAttribute("newsmodel", chuanzhiEbi.getAll(i, 6));
}
request.setAttribute("newsnum", j);
return "chuanzhiallf";
}
//分页显示kan
public String getAllFenkan(){
int j = chuanzhiEbi.getAllCount();
int jj=j-j%6;
HttpServletRequest request = ServletActionContext.getRequest();
int i = Integer.parseInt(request.getParameter("i"));
if (i >=j) {
request.setAttribute("i", jj);
request.setAttribute("newsmodel", chuanzhiEbi.getAll(jj, 6));
}else if(i<=0){
request.setAttribute("newsmodel", chuanzhiEbi.getAll(0, 6));
} else {
request.setAttribute("i", i);
request.setAttribute("newsmodel", chuanzhiEbi.getAll(i, 6));
}
request.setAttribute("newsnum", j);
return "kanallf";
}
}
Step Three. Writing configuration files
- Writing struts.xml (struts configuration file)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="myPackage" namespace="/" extends="struts-default">
<action name="user_*" class="com.allde.wo.user.action.UserAction"
method="{1}">
<result name="success" type="redirectAction">admin/user_getAll</result>
<result name="yuangong">manage_user.jsp</result>
<result name="adminf">manage_user.jsp</result>
<result name="delete" type="redirectAction">admin/user_getAll</result>
<result name="update">welcome.jsp</result>
<result name="byid">user_xiugai.jsp</result>
<result name="admin">admin/adminindex.jsp</result>
<result name="admin1">admin/adminindex1.jsp</result>
<result name="admin2">admin/adminindex2.jsp</result>
<result name="loginf">login.jsp</result>
</action>
<action name="news_*" class="com.allde.wo.news.action.NewsAction"
method="{1}">
<result name="success" type="redirectAction">admin/news_getAll</result>
<result name="newsallkan">manage_news_kan.jsp</result>
<result name="newsall">manage_news.jsp</result>
<result name="newsallqt">news.jsp</result>
<result name="allf">manage_news.jsp</result>
<result name="newsallq">new.jsp</result>
<result name="delete" type="redirectAction">admin/news_getAll</result>
<result name="update" type="redirectAction">admin/news_getAll</result>
<result name="byid">update_news.jsp</result>
<result name="xxid">info_news.jsp</result>
</action>
<action name="chuanzhi_*" class="com.allde.wo.chuanzhi.action.ChuanzhiAction"
method="{1}">
<result name="byid">update_chuanzhi.jsp</result>
<result name="newsallkan">kan_chuanzhi.jsp</result>
<result name="kanallf">kan_chuanzhi.jsp</result>
<result name="success" type="redirectAction">admin/chuanzhi_getAll</result>
<result name="newsall">manage_chuanzhi.jsp</result>
<result name="chuanzhiallf">manage_chuanzhi.jsp</result>
<result name="delete" type="redirectAction">admin/chuanzhi_getAll</result>
<result name="update" type="redirectAction">admin/chuanzhi_getAll</result>
</action>
<action name="chuxing_*" class="com.allde.wo.chuxing.action.ChuxingAction"
method="{1}">
<result name="newsallkan">kan_chuxing.jsp</result>
<result name="kanallf">kan_chuxing.jsp</result>
<result name="success" type="redirectAction">admin/chuxing_getAll</result>
<result name="addcl">add_chuxing.jsp</result>
<result name="newsall">manage_chuxing.jsp</result>
<result name="qtall">chuxing.jsp</result>
<result name="chuxingallf">manage_chuxing.jsp</result>
</action>
<action name="huogui_*" class="com.allde.wo.huogui.action.HuoguiAction"
method="{1}">
<result name="newsallkan">kan_huogui.jsp</result>
<result name="kanallf">kan_huogui.jsp</result>
<result name="success" type="redirectAction">admin/huogui_getAll</result>
<result name="newsall">manage_huogui.jsp</result>
<result name="huoguiallf">manage_huogui.jsp</result>
<result name="delete" type="redirectAction">admin/huogui_getAll</result>
</action>
<action name="luxian_*" class="com.allde.wo.luxian.action.LuxianAction"
method="{1}">
<result name="newsallkan">kan_luxian.jsp</result>
<result name="kanallf">kan_luxian.jsp</result>
<result name="success" type="redirectAction">admin/luxian_getAll</result>
<result name="newsall">manage_luxian.jsp</result>
<result name="luxianallf">manage_luxian.jsp</result>
<result name="delete" type="redirectAction">admin/luxian_getAll</result>
</action>
<action name="peitaohg_*" class="com.allde.wo.peitaohg.action.PeitaohgAction"
method="{1}">
<result name="newsallkan">kan_peitaohg.jsp</result>
<result name="kanallf">kan_peitaohg.jsp</result>
<result name="success" type="redirectAction">admin/peitaohg_getAll</result>
<result name="addczhg">add_peitaohg.jsp</result>
<result name="newsall">manage_peitaohg.jsp</result>
<result name="qtall">peitaohg.jsp</result>
<result name="allf">manage_peitaohg.jsp</result>
</action>
</package>
</struts>
- Writing applicationContext.xml (spring configuration file)
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- dao -->
<bean id="userDao" class="com.allde.wo.user.dao.impl.UserDaoHibernateImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="newsDao" class="com.allde.wo.news.dao.impl.NewsDaoHibernateImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="chuanzhiDao" class="com.allde.wo.chuanzhi.dao.impl.ChuanzhiDaoHibernateImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="chuxingDao" class="com.allde.wo.chuxing.dao.impl.ChuxingDaoHibernateImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="huoguiDao" class="com.allde.wo.huogui.dao.impl.HuoguiDaoHibernateImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="luxianDao" class="com.allde.wo.luxian.dao.impl.LuxianDaoHibernateImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="peitaohgDao" class="com.allde.wo.peitaohg.dao.impl.PeitaohgDaoHibernateImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- ebi -->
<bean id="userEbi" class="com.allde.wo.user.business.ebo.UserEbo">
<property name="userDao" ref="userDao" />
</bean>
<bean id="newsEbi" class="com.allde.wo.news.business.ebo.NewsEbo">
<property name="newsDao" ref="newsDao" />
</bean>
<bean id="chuanzhiEbi" class="com.allde.wo.chuanzhi.business.ebo.ChuanzhiEbo">
<property name="chuanzhiDao" ref="chuanzhiDao" />
</bean>
<bean id="chuxingEbi" class="com.allde.wo.chuxing.business.ebo.ChuxingEbo">
<property name="chuxingDao" ref="chuxingDao" />
</bean>
<bean id="luxianEbi" class="com.allde.wo.luxian.business.ebo.LuxianEbo">
<property name="luxianDao" ref="luxianDao" />
</bean>
<bean id="huoguiEbi" class="com.allde.wo.huogui.business.ebo.HuoguiEbo">
<property name="huoguiDao" ref="huoguiDao" />
</bean>
<bean id="peitaohgEbi" class="com.allde.wo.peitaohg.business.ebo.PeitaohgEbo">
<property name="peitaohgDao" ref="peitaohgDao" />
</bean>
<!-- action -->
<!--transaction -->
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 那些类那些方法使用事务 -->
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(* com.allde.wo.*.business.ebo.*.*(..)) or execution(* com.allde.framework.business.ebo.*.*(..)) " />
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
</aop:config>
<!-- 事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
</beans>
- Writing hibernate.cfg.xml (hibernate configuration file)
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/matouczjihggl_web</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property><!-- -->
<mapping resource="com/allde/wo/user/model/UserModel.hbm.xml" />
<mapping resource="com/allde/wo/news/model/NewsModel.hbm.xml" />
<mapping resource="com/allde/wo/chuanzhi/model/ChuanzhiModel.hbm.xml" />
<mapping resource="com/allde/wo/chuxing/model/ChuxingModel.hbm.xml" />
<mapping resource="com/allde/wo/huogui/model/HuoguiModel.hbm.xml" />
<mapping resource="com/allde/wo/luxian/model/LuxianModel.hbm.xml" />
<mapping resource="com/allde/wo/peitaohg/model/PeitaohgModel.hbm.xml" />
</session-factory>
</hibernate-configuration>
Step Fore. Writing configuration files
- Writing Write jsp front page
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
</body>
</html>