Struts2+Spring3+Hibernate3 v3.0
22157 ワード
Struts2+Spring3+Hibernate3
バージョン情報:
1.オブジェクトモデルのプライマリ・キーは、Userでidとしてusernameを使用するなど、独自のプライマリ・キー・マッピングを採用する
2.Hibernate(JPA)注記によるマッピング
3.Spring用の注記を構成
4.Struts用のプロファイル
5.ページ用のJqueryフレーム
注意:web部分はdepartmentモジュールのみを実現し、簡単な実現をしただけです.
Department.java
applicationContext.xml
struts.xml
struts_actions.xml
web.xml
DepartmentAction
addDepartment.jsp
updateDepartment.jsp
listDepartment.jsp
department.js
バージョン情報:
1.オブジェクトモデルのプライマリ・キーは、Userでidとしてusernameを使用するなど、独自のプライマリ・キー・マッピングを採用する
2.Hibernate(JPA)注記によるマッピング
3.Spring用の注記を構成
4.Struts用のプロファイル
5.ページ用のJqueryフレーム
注意:web部分はdepartmentモジュールのみを実現し、簡単な実現をしただけです.
Department.java
package com.jvwl.model;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;
/*
*
*/
@Entity
@Table(name="t_department")
public class Department {
private String departmentId;
private String name;
private Set<Grade> grades;
private int version;
@Column(length=10)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Version
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
@OneToMany(cascade={CascadeType.REMOVE},mappedBy="department")
public Set<Grade> getGrades() {
return grades;
}
public void setGrades(Set<Grade> grades) {
this.grades = grades;
}
@Id @Column(length=20)
public String getDepartmentId() {
return departmentId;
}
public void setDepartmentId(String departmentId) {
this.departmentId = departmentId;
}
}
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:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.jvwl.dao.impl,com.jvwl.service.impl,com.jvwl.action" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/student_info_system"/>
<property name="username" value="root"/>
<property name="password" value="jerval"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.jvwl.model</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution(* com.jvwl.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
</aop:config>
</beans>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.devMode" value="true" />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.ui.theme" value="jvwl" />
<include file="struts_actions.xml" />
<package name="jvwl" extends="json-default">
<default-action-ref name="loginPage" />
<global-results>
<result name="login" type="redirect">/login.jsp</result>
<result name="exception">/error.jsp</result>
</global-results>
<action name="loginPage">
<result>/login.jsp</result>
</action>
</package>
</struts>
struts_actions.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="login" extends="jvwl" namespace="/">
<action name="loginAction" class="loginAction">
<result name="loginSuccess">/main.jsp</result>
<result name="loginFail">/login.jsp</result>
</action>
</package>
<package name="department" extends="jvwl" namespace="/department">
<action name="departmentAction" class="departmentAction">
<result name="add">/department/addDepartment.jsp</result>
<result name="update">/department/updateDepartment.jsp</result>
<result name="text" type="stream">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
<result name="pagination">/department/listDepartment.jsp</result>
<result name="department">/department/updateDepartment.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<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>
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
DepartmentAction
package com.jvwl.action;
import java.io.ByteArrayInputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.jvwl.bean.Pagination;
import com.jvwl.model.Department;
import com.jvwl.model.User;
import com.jvwl.service.DepartmentService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Component("departmentAction")
@Scope("prototype")
public class DepartmentAction extends ActionSupport implements
ModelDriven<Department>, SessionAware {
private static final long serialVersionUID = 4233464072288035445L;
private DepartmentService departmentService;
private Department department=new Department();
private Map<String, Object> session;
private ByteArrayInputStream inputStream;
private int pageSize=20;
private int pageNum=1;
private Pagination<Department> pagination;
public ByteArrayInputStream getInputStream() {
return inputStream;
}
public String getOperateUserName() {
return ((User) session.get("user")).getUsername();
}
public String add() {
return "add";
}
public String addSave() {
departmentService.addDepartment(department, getOperateUserName());
inputStream = new ByteArrayInputStream("success ".getBytes());
return "text";
}
public String update() {
department = departmentService.findDepartment(department.getDepartmentId());
return "update";
}
public String updateSave() {
departmentService.updateDepartment(department, getOperateUserName());
inputStream = new ByteArrayInputStream("success ".getBytes());
return "text";
}
public String delete() {
List<Serializable> ids = new ArrayList<Serializable>();
ids.add(department.getDepartmentId());System.out.println(ids);
departmentService.deleteDepartment(ids, getOperateUserName());
findAll();
return "pagination";
}
public String findOne() {
department = departmentService.findDepartment(department.getDepartmentId());
return "department";
}
public String findAll() {
pagination = departmentService.findDepartments(null, pageNum, pageSize);
return "pagination";
}
@Override
public Department getModel() {
return department;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Resource
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
public void setDepartment(Department department) {
this.department = department;
}
public Department getDepartment() {
return department;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public Pagination<Department> getPagination() {
return pagination;
}
}
addDepartment.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
</head>
<body>
<form name="form1" method="post" action="departmentAction!addSave">
<table width="600" border="0">
<tr>
<td align="right"> </td>
<td><input name="departmentId" type="text" id="departmentId" size="18"></td>
</tr>
<tr>
<td align="right"> </td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value=" "></td>
</tr>
</table>
<p> </p>
</form>
</body>
</html>
updateDepartment.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>
</head>
<body>
<form name="form1" method="post" action="departmentAction!updateSave">
<table width="600" border="0">
<tr>
<td align="right">
</td>
<td><input id="version" name="version" type="hidden" value="<s:property value='department.version'/>">
<input name="departmentId" type="text" id="departmentId" size="18"
value="<s:property value='department.departmentId'/>">
</td>
</tr>
<tr>
<td align="right">
</td>
<td>
<input type="text" name="name" id="name" value="<s:property value='department.name'/>">
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" name="button" id="button" value=" ">
</td>
</tr>
</table>
<p>
</p>
</form>
</body>
</html>
listDepartment.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script type="text/javascript" src="../js/jquery-1.4.2.js"></script>
<script type="text/javascript" src="../js/jquery.validate.js"></script>
<script type="text/javascript" src="department.js"></script>
</head>
<body>
<table width="100%">
<thead>
<tr>
<th>
</th>
<th colspan="2">
</th>
<th>
<a href="javascript:add()"><img alt=" " src="../images/addoffer.png" /></a>
</th>
</tr>
<tr>
<th bgcolor="#C1E7FF">
</th>
<th colspan="2" bgcolor="#C1E7FF">
</th>
<th bgcolor="#C1E7FF">
</th>
</tr>
</thead>
<tbody id="dataArea">
<s:iterator value="pagination.list" var="department">
<tr>
<s:set var="id" value="#department.departmentId" />
<td>
<s:property value="#id" />
</td>
<td colspan="2">
<s:property value="#department.name" />
</td>
<td>
<a href="javascript:update('<s:property value='#id'/>')"> </a> |
<a href="javascript:del('<s:property value='#id'/>')"> </a>
</td>
</tr>
</s:iterator>
<tr>
<td bgcolor="#C1E7FF">
</td>
<td colspan="2" bgcolor="#C1E7FF">
</td>
<td bgcolor="#C1E7FF">
</td>
</tr>
</tbody>
<tfoot id="footArea">
<tr>
<s:set var="pageNum" value="department.pageNum" />
<td colspan="2" bgcolor="#C1E7FF">
<s:property value="#pageNum" />
,
<s:property value="department.maxPages" />
|
<s:property value="department.maxElements" />
<a href=""></a>
</td>
<td colspan="2" bgcolor="#C1E7FF">
| | |
</td>
</tr>
</tfoot>
</table>
</body>
</html>
department.js
function add() {
window.open("departmentAction!add", "aaaa", "width=400,height=400");
}
function update(id) {
window.open("departmentAction!update?departmentId=" + id, "bbb", "width=400,height=400");
}
function del(id) {
$.ajax( {
type : "POST",
url : "departmentAction!delete",
cache : false,
data : "departmentId=" + id,
success : function(data) {
alert("ok");
}
});
}