2010年3月23日火曜日

使用Struts2,Spring2.5,SpringJDBCTemplate 构建小系统快速开发的组合(原创)

○jar包
1.与Struts2和Spring一定可能有关系的jar包(未验证)
activation.jar
commons-beanutils-1.8.0.jar
commons-collections-3.2.jar
commons-dbcp-1.2.2.jar
commons-dbutils-1.1.jar
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
commons-pool-1.4.jar

2.与Struts2和Spring一定有关系的jar包
freemarker-2.3.8.jar
ognl-2.6.11.jar
spring2.5.5.jar
struts2-core-2.0.14.jar
struts2-spring-plugin-2.0.11.1.jar
xwork-2.0.7.jar

3.proxool数据库连接池关联jar包
proxool-0.9.1.jar
proxool-cglib.jar

4.其他
mysql-connector-java-5.1.7-bin.jar
log4j-1.2.15.jar

○放到WEB-INF下的配置文件
web.xml
applicationContext.xml
context.properties
daoContext.xml

1、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml
,/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>

<session-config>
<session-timeout>30</session-timeout>
</session-config>
<error-page>
<error-code>500</error-code>
<location>/error500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>

</web-app>

2、applicationContext.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans default-autowire="autodetect">

<!-- Dao -->
<bean id="adminListDao" class="jp.co.helios.password.dao.AdminListDao">
<property name="npjt" ref="npjt" />
</bean>

<bean id="volumeDao" class="jp.co.helios.password.dao.VolumeDao">
<property name="npjt" ref="npjt" />
</bean>

<!-- Util -->
<bean id="interceptorUtil" class="jp.co.helios.password.interceptor.InterceptorUtil">
<property name="adminListDao">
<ref bean="adminListDao" />
</property>
</bean>

</beans>

3、context.properties

proxool.driver=com.mysql.jdbc.Driver
proxool.url=jdbc:mysql://192.168.100.83:3306/tf_fsm?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
proxool.username=root
proxool.password=config
proxool.alias=dbpool
proxool.maximumConnectionCount=5
proxool.minimumConnectionCount=2

4、daoContext.xml
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="byName" default-lazy-init="false"
default-dependency-check="none">

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/context.properties</value>
</property>
</bean>

<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver">
<value>${proxool.driver}</value>
</property>
<property name="driverUrl">
<value>${proxool.url}</value>
</property>
<property name="user">
<value>${proxool.username}</value>
</property>
<property name="password">
<value>${proxool.password}</value>
</property>
<property name="alias">
<value>${proxool.alias}</value>
</property>
<property name="maximumConnectionCount">
<value>${proxool.maximumConnectionCount}</value>
</property>
<property name="minimumConnectionCount">
<value>${proxool.minimumConnectionCount}</value>
</property>
</bean>

<bean id="npjt"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>

</beans>

○Struts2のStruts.xml
<?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>
<constant name="struts.action.extension" value="do" />
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.i18n.reload" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.objectFactory" value="spring" />

<package name="helios-home" extends="struts-default">
<interceptors>
<interceptor name="loginStack"
class="jp.co.helios.password.interceptor.LoginSessionInterceptor" />
<interceptor-stack name="mystack">
<interceptor-ref name="loginStack" />
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="mystack"></default-interceptor-ref>
<default-action-ref name="noAction"></default-action-ref>

<global-results>
<result name="globaluserLogin" type="redirect">gouserindex.do
</result>
<result name="userSideSessionTimeOutResult">/login.jsp</result>
<result name="error" type="chain">exceptionDoAction
</result>
<result name="sql" type="chain">exceptionDoAction
</result>
<result name="nullPoint" type="chain">exceptionDoAction
</result>
<result name="indexOutOfBoundsException" type="chain">
exceptionDoAction</result>
</global-results>

<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception" />
<exception-mapping result="sql" exception="java.sql.SQLException" />
<exception-mapping result="nullPoint"
exception="java.lang.NullPointerException" />
<exception-mapping result="indexOutOfBoundsException"
exception="java.lang.IndexOutOfBoundsException" />
</global-exception-mappings>

<action name="userLoginInterceptAction"
class="jp.co.helios.password.action.UserLoginInterceptAction" />

<action name="exceptionDoAction" class="jp.co.helios.password.action.ExceptionDoAction">
<result>unknownError.jsp</result>
</action>

<action name="noAction">
<result type="redirect">goto404.do</result>
</action>

<action name="goto404" class="jp.co.helios.password.action.CommonAction">
<result name="success">/error404.jsp</result>
</action>


<action name="login" class="jp.co.helios.password.action.CommonAction">
<result name="success">/login.jsp</result>
</action>

<action name="gouserindex" class="jp.co.helios.password.action.CommonAction">
<result>/goIndex.jsp</result>
</action>
<action name="logoutAction" class="jp.co.helios.password.action.LogoutAction">
<result name="usersideLogin">/login.jsp</result>
</action>

<action name="volumeListAction" class="jp.co.helios.password.action.VolumeListAction">
<result name="success">/volumeList.jsp</result>
</action>

</package>
</struts>

○VolumeListAction.java

package jp.co.helios.password.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jp.co.helios.password.dao.VolumeDao;
import jp.co.helios.password.dto.PageParamDTO;
import jp.co.helios.password.dto.SessionDTO;
import jp.co.helios.password.dto.VolumeListDTO;
import jp.co.helios.password.util.CommonUtil;
import jp.co.helios.password.util.PageOperateUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class VolumeListAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private Map session;
private SessionDTO sessionDTO = new SessionDTO();
private List list = new ArrayList();
private VolumeDao volumeDao;
public void setVolumeDao(VolumeDao volumeDao) {
this.volumeDao = volumeDao;
}
private int rowCount;
private int currentPage;
private int totalPage;
private int firstpage;
private int endPage;
private int perItemPage;
private int begin;
private int end;
private int perItemsPage;
private String sortName;

public String execute() throws Exception {

ActionContext ctx = ActionContext.getContext();
session = ctx.getSession();
SessionDTO sessionDTO = (SessionDTO)session.get("sessionDTO");
rowCount = volumeDao.getVolumeListCount();
PageParamDTO pageParamDTO = new PageParamDTO();
pageParamDTO.setCurrentPage(currentPage);
pageParamDTO.setPerItemPage(perItemPage);
pageParamDTO.setRowCount(rowCount);
pageParamDTO = PageOperateUtil.pageParamCount(pageParamDTO);
int firstindex = pageParamDTO.getFirstindex();
perItemPage = pageParamDTO.getPerItemPage();
String sortOrder = null;
//sort
if(!CommonUtil.isInvalid(sortName)){
PageOperateUtil.sortCount(sortName,sessionDTO);
sortName = sessionDTO.getSortName();
sortOrder = sessionDTO.getSortOrder();
}
VolumeListDTO volumeListDTO = new VolumeListDTO();
volumeListDTO.setFirstindex(firstindex);
volumeListDTO.setPerItemPage(perItemPage);
volumeListDTO.setSortName(sortName);
volumeListDTO.setSortOrder(sortOrder);
if(rowCount!=0){
list = volumeDao.getVolumeList(volumeListDTO);
}
setFirstpage(firstindex + 1);
setEndPage(firstindex + perItemPage);
setCurrentPage(pageParamDTO.getCurrentPage());
setTotalPage(pageParamDTO.getTotalPage());
pageParamDTO = PageOperateUtil.beginEndCount(pageParamDTO);
begin = pageParamDTO.getBegin();
end = pageParamDTO.getEnd();

return SUCCESS;
}
public Map getSession() {
return session;
}
public void setSession(Map session) {
this.session = session;
}
public SessionDTO getSessionDTO() {
return sessionDTO;
}
public void setSessionDTO(SessionDTO sessionDTO) {
this.sessionDTO = sessionDTO;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getRowCount() {
return rowCount;
}
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getFirstpage() {
return firstpage;
}
public void setFirstpage(int firstpage) {
this.firstpage = firstpage;
}
public int getEndPage() {
return endPage;
}
public void setEndPage(int endPage) {
this.endPage = endPage;
}
public int getPerItemPage() {
return perItemPage;
}
public void setPerItemPage(int perItemPage) {
this.perItemPage = perItemPage;
}
public int getBegin() {
return begin;
}
public void setBegin(int begin) {
this.begin = begin;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public int getPerItemsPage() {
return perItemsPage;
}
public void setPerItemsPage(int perItemsPage) {
this.perItemsPage = perItemsPage;
}
public String getSortName() {
return sortName;
}
public void setSortName(String sortName) {
this.sortName = sortName;
}
}


○VolumeDao.java
package jp.co.helios.password.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import jp.co.helios.password.dto.VolumeListDTO;

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

public class VolumeDao {

private NamedParameterJdbcTemplate npjt = null;

public void setNpjt(NamedParameterJdbcTemplate npjt) {
this.npjt = npjt;
}

public int getVolumeListCount(){
StringBuffer sb = new StringBuffer();
sb.append("select count(1) ");
sb.append(" from folder_list fl, ");
sb.append(" (select fl.src_helios_volume_id, CONCAT(hv.path,',', hv.AFPName,',',hv.SMBName) as srcVolume ");
sb.append(" from folder_list fl ,helios_volume hv ");
sb.append(" where ");
sb.append(" fl.src_helios_volume_id = hv.helios_volume_id) t1, ");
sb.append(" (select fl.dst_helios_volume_id, CONCAT(hv.path,',', hv.AFPName,',',hv.SMBName) as dstVolume ");
sb.append(" from folder_list fl ,helios_volume hv ");
sb.append(" where ");
sb.append(" fl.dst_helios_volume_id = hv.helios_volume_id) t2 ");
sb.append(" where fl.src_helios_volume_id = t1.src_helios_volume_id ");
sb.append(" and fl.dst_helios_volume_id = t2.dst_helios_volume_id ");
return npjt.getJdbcOperations().queryForInt(sb.toString());
}

public List getVolumeList(VolumeListDTO volumeListDTO){

Integer firstindex = volumeListDTO.getFirstindex();
Integer perItemPage = volumeListDTO.getPerItemPage();
String sortName = volumeListDTO.getSortName();
String sortOrder = volumeListDTO.getSortOrder();

StringBuffer sb = new StringBuffer();
Map paramMap = new HashMap();
sb.append(" select t1.srcVolume as srcVolumeName,t2.dstVolume as dstVolumeName," +
"fl.src_folder as srcFolder,fl.operation as operation,fl.dst_folder as dstFolder ");
sb.append(" from folder_list fl, ");
sb.append(" (select fl.src_helios_volume_id, CONCAT(hv.path,',', hv.AFPName,',',hv.SMBName) as srcVolume ");
sb.append(" from folder_list fl ,helios_volume hv ");
sb.append(" where ");
sb.append(" fl.src_helios_volume_id = hv.helios_volume_id) t1, ");
sb.append(" (select fl.dst_helios_volume_id, CONCAT(hv.path,',', hv.AFPName,',',hv.SMBName) as dstVolume ");
sb.append(" from folder_list fl ,helios_volume hv ");
sb.append(" where ");
sb.append(" fl.dst_helios_volume_id = hv.helios_volume_id) t2 ");
sb.append(" where fl.src_helios_volume_id = t1.src_helios_volume_id ");
sb.append(" and fl.dst_helios_volume_id = t2.dst_helios_volume_id ");
sb.append(" limit :firstindex,:perItemPage ");
paramMap.put("firstindex", firstindex);
paramMap.put("perItemPage", perItemPage);

List volumeList = new ArrayList();
List rows = npjt.queryForList(sb.toString(), paramMap);
Iterator it = rows.iterator();
while(it.hasNext()) {
Map userMap = (Map) it.next();
VolumeListDTO dto = new VolumeListDTO();
dto.setSrcVolumeName(userMap.get("srcVolumeName").toString());
dto.setSrcVolumeName(userMap.get("dstVolumeName").toString());
dto.setSrcFolder(userMap.get("srcFolder").toString());
dto.setOperation(userMap.get("operation").toString());
dto.setDstFolder(userMap.get("dstFolder").toString());
volumeList.add(dto);
}
return volumeList;
}
}

0 件のコメント: