Struts2+Spring3+Hibernate3 v4.0

28370 ワード

Struts2+Spring3+Hibernate3
 
1.オブジェクトモデルのプライマリ・キーは、Userでidとしてusernameを使用するなど、独自のプライマリ・キー・マッピングを採用する
2.JPA(Hibernate)注記によるマッピング
3.Spring用の注記を構成
4.Struts用のプロファイル
5.ページ用のJqueryフレーム
 
注意:web部分はdepartmentモジュールのみを実現し、簡単な実現をしただけです.
META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
  
  <persistence-unit name="jvwl" transaction-type="RESOURCE_LOCAL">
  </persistence-unit>
</persistence>

 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="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="showSql" value="true" />
				<property name="generateDdl" value="true" />
				<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
			</bean>
		</property>
	</bean>
	
	<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<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.*.*(..))" />
		<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">
		<interceptors>
			<interceptor name="security" class="com.jvwl.interceptor.SecurityInterceptor" />
			<interceptor-stack name="secureStack">
				<interceptor-ref name="security" />
				<interceptor-ref name="defaultStack" />
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="secureStack"></default-interceptor-ref>
		<default-action-ref name="loginPage" />
		<global-results>
			<result name="login" type="redirectAction">loginPage</result>
			<result name="exception">/error.jsp</result>
		</global-results>
		<global-exception-mappings>
            <exception-mapping exception="com.jvwl.exception.BusinessException" result="exception"/>
            <exception-mapping exception="java.lang.Exception" result="exception"/>
        </global-exception-mappings>
		
		<action name="loginPage">
			<result>/login.jsp</result>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>
	</package>
</struts>

 messages.properties
struts.messages.invalid.token=\u8868\u5355\u5DF2\u7ECF\u88AB\u5904\u7406\u6216\u6CA1\u6709\u4EE4\u724C\u6807\u8BC6\uFF0C\u8BF7\u91CD\u8BD5\uFF01
struts.internal.invalid.token=\u8868\u5355\u4EE4\u724C {0} \u4E0D\u80FD\u4E0E\u4F1A\u8BDD\u4E2D\u7684\u4EE4\u724C {1}\u5339\u914D\uFF01

struts.messages.bypass.request=\u5C94\u8DEF {0}/{1}
struts.messages.current.file=\u6587\u4EF6 {0} {1} {2} {3}
struts.messages.invalid.file=\u4E0D\u80FD\u4E3A {0}\u627E\u5230\u4E00\u4E2A\u6587\u4EF6\u540D\uFF0C\u8BF7\u68C0\u67E5\u63D0\u4EA4\u4FE1\u606F\u7684\u6709\u6548\u6027\uFF01
struts.messages.invalid.content.type=\u4E0D\u80FD\u627E\u5230 {0}\u7684\u4E00\u4E2A\u4E0A\u4E0B\u6587\u7C7B\u578B. \u8BF7\u68C0\u67E5\u63D0\u4EA4\u4FE1\u606F\u7684\u6709\u6548\u6027\uFF01
struts.messages.removing.file=\u79FB\u9664\u6587\u4EF6 {0} {1}
struts.messages.error.uploading=\u4E0A\u4F20\u51FA\u9519\: {0}
struts.messages.error.file.too.large=\u6587\u4EF6\u592A\u5927\: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=\u4E0A\u4E0B\u6587\u7C7B\u578B\u4E0D\u5141\u8BB8\: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=\u6587\u4EF6\u540E\u7F00\u4E0D\u5141\u8BB8\: {0} "{1}" "{2}" {3}

devmode.notification=\u5F00\u53D1\u8005\u901A\u544A (\u8BBE\u7F6E struts.devMode\u4E3A false\u53EF\u5173\u95ED\u6B64\u6D88\u606F)\:\r
{0} xwork.error.action.execution=Action\u8C03\u7528\u65F6\u51FA\u9519\uFF01 xwork.exception.missing-action=\u627E\u4E0D\u5230\u540D\u4E3A{0}\u7684Action\u6620\u5C04\uFF01 xwork.exception.missing-package-action=\u6CA1\u6709\u547D\u540D\u7A7A\u95F4\u4E3A {0} \u548CAction\u540D\u4E3A{1}\u7684\u6620\u5C04\uFF01 xwork.default.invalid.fieldvalue=\ "{0}"\u5B57\u6BB5\u7684\u503C\u65E0\u6548\uFF01

 SecurityInterceptor.java
package com.jvwl.interceptor;

import java.util.Map;

import com.jvwl.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class SecurityInterceptor extends AbstractInterceptor {

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Map session = ActionContext.getContext().getSession();
		User user = (User) session.get("user");
		if (user != null) {
			return invocation.invoke();
		}
		return "login";
	}

}

 BusinessException.java
package com.jvwl.exception;

public class BusinessException {
	private static final long serialVersionUID = -7852715647562898602L;

	@SuppressWarnings("unchecked")
	public static RuntimeException notExistException(Class clazz, Object id) {
		throw new RuntimeException("         : " + id + "  "+clazz.getSimpleName());
	}
	
	@SuppressWarnings("unchecked")
	public static RuntimeException existException(Class clazz, Object id) {
		throw new RuntimeException("          : " + id + "  "+clazz.getSimpleName());
	}

	public static RuntimeException systemException(String info) {
		throw new RuntimeException("      : " + info);
	}
}

 xwork-conversion.properties
java.util.Date=com.jvwl.converter.DateConvertor

 DateConvertor.java
package com.jvwl.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.jvwl.exception.BusinessException;

public class DateConvertor extends StrutsTypeConverter {

	@SuppressWarnings("unchecked")
	@Override
	public Object convertFromString(Map map, String[] values, Class toClass) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		Date date =null;
		try {
			date = sdf.parse("");
		} catch (ParseException e) {
			throw BusinessException.systemException("      !");
		}
		return date;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String convertToString(Map map, Object object) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return sdf.format(object);
	}

}

  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>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</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>Spring OpenEntityManagerInViewFilter</filter-name>
		<filter-class>
			org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>Spring OpenEntityManagerInViewFilter</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>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="button" id="button" value="  "></td>
    </tr>
  </table>
<p>&nbsp;</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>
						&nbsp;
					</td>
					<td>
						&nbsp;
					</td>
				</tr>
				<tr>
					<td>
						&nbsp;
					</td>
					<td>
						<input type="submit" name="button" id="button" value="  ">
					</td>
				</tr>
			</table>
			<p>
				&nbsp;
			</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>
						&nbsp;
					</th>
					<th colspan="2">
						&nbsp;
					</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">
						&nbsp;
					</td>
					<td colspan="2" bgcolor="#C1E7FF">
						&nbsp;
					</td>
					<td bgcolor="#C1E7FF">
						&nbsp;
					</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");
		}
	});
}