项目环境搭建一般是由项目的架构师或项目组长,搭建好的项目中,java代码和配置文件以及测试代码应该是分离。各个框架的配置文件也要分开。
基本环境整体的效过:
下面开始建立项目,搭建环境。
一、建立好项目后,需要再建立两个单独的源文件夹,分别为test和config
在config下分别建立struts,spring和hibernate三个包,以便后面分别放入这三部分的配置。
·
二、然后加入Spring支持。
Spring和Struts的配置文件也不只一个,而是要根据功能来拆分为多个。
Spring的配置文件最好拆分为以下五个:
1) -hibernate.xml:放入固定的数据库连接等相关配置
2) -dao.xml:放入DAO相关配置
3) -service.xml:放入Service相关配置
4) -struts.xml:放入Action相关配置
5) -transaction.xml:放入固定的事务处理相关配置。
三、加入Hibernate和struts的支持
将Hibernate部分的配置自动生成到-hibernate.xml配置文件中。
在-hibernate.xml中补充其他配置。
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver">
</property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL">
</property>
<property name="username" value="sunxun"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
<prop key="hibernate.format_sql">
true
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>hibernate/NewsType.hbm.xml</value>
<value>hibernate/News.hbm.xml</value>
</list>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
//*
*其中dataSource数据源,主要是配置与数据库连接的操作。这里是Oracle
*SessionFactory是连接池,主要配置的Hibernate的属性,比如show_sql,format_sql
*还有一个生成表的映射文件
*
*//
在-transaction.xml中补充三个<bean>的配置(标准的三段式)。
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <!-- 配置事务处理的方法和方式 --> <property name="transactionAttributes"> <props> <!-- 表示所有方法都要关闭连接,并进行事务处理. PROPAGATION_REQUIRED:如果之前有事务,则将当前操作合并到之前的事务中,如果之前没有事务,则开始一个新的事务。 PROPAGATION_REQUIRED_NEW:无论之前是否有事务,都开始一个新的事务。 PROPAGATION_REQUIRED_NEVER:不使用事务处理,使用自动提交方式。 --> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*ServiceImpl</value> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean>
四、Struts的配置文件拆分方式:
1) Struts.xml:放入一些公共的配置和包含信息
<struts> <include file="struts/struts-root.xml"></include> <include file="struts/struts-front.xml"></include> <include file="struts/struts-back.xml"></include> <package name="my-default" extends="struts-default" abstract="true"> <!-- 全局文件大小限制方式,一般不推荐 <interceptors> <interceptor-stack name="my-default-stack"> <interceptor-ref name="fileUpload"> <param name="maximumSize">700000</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> --> <global-results> <result name="forward">/forward.jsp</result> <result name="input">/error.jsp</result> </global-results> </package> </struts>
2) Struts-root.xml:里面配置所有不需要登陆就能访问的Action配置。
3) Struts-front.xml:前台普通用户登陆后才能处理的相关功能
4) Struts-back.xml:后台管理员登陆后才能处理的相关功能。
五、将项目的包也要按照固定的格式来建立出来,包名默认为: 公司名.项目名.功能
将pojo映射生成。
src下需要的包:
cn.itcast.ssh.back.action;
cn.itcast.ssh.back.service.impl;
cn.itcast.ssh.dao.impl;
cn.itcast.ssh.filter;
cn.itcast.ssh.front.action;
cn.itcast.ssh.front.service.action;
cn.itcast.ssh.util;
六、编写web.xml 的监听
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
最后将一些公共的文件拷贝到项目里,如公共的IDAO
分页的组件:split_page.jsp
<html> <head> <base href="<%=basePath%>"> </head> <body> <%-- 使用说明: 列表时,将以下内容粘贴到需要分页显示的位置 <jsp:include page="/split_page_plugin.jsp"> <jsp:param value="${pageNo}" name="pageNo"/> <jsp:param value="${pageSize}" name="pageSize"/> <jsp:param value="${column}" name="column"/> <jsp:param value="${keyword}" name="keyword"/> <jsp:param value="${count}" name="count"/> <jsp:param value="news!list.action" name="URL"/> <jsp:param value="title:标题|content:内容" name="columnData"/> <jsp:param value="1" name="pageStyle"/> </jsp:include> 其中: URL为查询列表的路径. columnData为搜索功能的选项处理,格式: 字段名:显示文字|字段名2:显示文字2..... pageStyle为分页组件样式,其中1 为数字版,2为下拉列表,3或默认为文本框 --%> <br/> <% int pageNo = Integer.parseInt(request.getParameter("pageNo")); int pageSize = Integer.parseInt(request.getParameter("pageSize")); int count = Integer.parseInt(request.getParameter("count")); int allPages = (count - 1) / pageSize + 1; String column = request.getParameter("column"); String keyword = request.getParameter("keyword"); String URL = request.getParameter("URL"); %> <form action="<%=URL %>" id="split_form" method="post" onsubmit="return checkPageNo();"> <% String pageStyle = request.getParameter("pageStyle"); if ("1".equals(pageStyle)) { %> <input type="button" value="<<" <%=pageNo==1?"disabled":"" %> onclick="changePage(1);"/> <input type="button" value="<" <%=pageNo==1?"disabled":"" %> onclick="changePage(<%=pageNo-1 %>);"/> <% if (pageNo - 2 > 1 ) { %> ... <% } for (int i = pageNo - 2; i <= pageNo + 2 && i <= allPages;i++) { if (i == pageNo) { %> <font color="red"><%=i %></font> <% } else if (i > 0) { %> <a href="javascript:changePage(<%=i %>);"><%=i %></a> <% } } if (pageNo + 2 < allPages) { %> ... <% } %> <input type="button" value=">" <%=allPages==pageNo?"disabled":"" %> onclick="changePage(<%=pageNo+1 %>)"/> <input type="button" value=">>" <%=allPages==pageNo?"disabled":"" %> onclick="changePage(<%=allPages %>);"/> <input type="hidden" name="pageNo" id="pageNo" value="<%=pageNo %>"/> <% } else if ("2".equals(pageStyle)) { %> <input type="button" value="首页" <%=pageNo==1?"disabled":"" %> onclick="changePage(1);"/> <input type="button" value="上一页" <%=pageNo==1?"disabled":"" %> onclick="changePage(<%=pageNo - 1 %>);"/> <input type="button" value="下一页" <%=allPages == pageNo?"disabled":"" %> onclick="changePage(<%=pageNo + 1 %>);"/> <input type="button" value="尾页" <%=allPages == pageNo?"disabled":"" %> onclick="changePage(<%=allPages %>);"/> 跳转到 <select id="pageNo" name="pageNo" onchange="changePage(this.value);"> <% for (int i = 1; i <= allPages ;i++) { %> <option value="<%=i %>" <%=i==pageNo?"selected":"" %>><%=i %></option> <% } %> </select> 页 <% } else { %> <input type="button" value="首页" <%=pageNo==1?"disabled":"" %> onclick="changePage(1);"/> <input type="button" value="上一页" <%=pageNo==1?"disabled":"" %> onclick="changePage(<%=pageNo - 1 %>);"/> <input type="button" value="下一页" <%=allPages == pageNo?"disabled":"" %> onclick="changePage(<%=pageNo + 1 %>);"/> <input type="button" value="尾页" <%=allPages == pageNo?"disabled":"" %> onclick="changePage(<%=allPages %>);"/> 跳转到 <input type="text" id="pageNo" name="pageNo" size="3" maxlength="3" value="<%=pageNo %>"/> 页 <input type="submit" value="Go"/> <% } %> <% int[] allSize = {1,2,5,10,20}; %> 每页显示 <select name="pageSize" id="pageSize" onchange="changePage(1);"> <% for (int i = 0;i < allSize.length;i++) { %> <option value="<%=allSize[i] %>" <%=allSize[i]==pageSize?"selected":"" %>><%=allSize[i] %></option> <% } %> </select> 条数据 <br/> <select name="column" id="column"> <% String columnData = request.getParameter("columnData"); String[] dataValues = columnData.split("\\|"); for (int i = 0;i < dataValues.length;i++) { String[] values = dataValues[i].split(":"); %> <option value="<%=values[0] %>" <%=values[0].equals(column)?"selected":"" %>><%=values[1] %></option> <% } %> </select> <input type="text" name="keyword" value="<%=keyword %>"/> <input type="submit" value="搜索"/> </form> <script type="text/javascript"> function changePage(newPage) { // 改变页数 document.getElementById("pageNo").value = newPage; // 提交表单,改变显示内容 document.getElementById("split_form").submit(); } function checkPageNo() { // 取得当前输入的页数 var pageNo = document.getElementById("pageNo").value; // 判断是否是合法的数字 // 在js里,使用 /^格式$/.test(内容) 来测试内容是否匹配某个格式 // \d(数字),\w(字母,数字或下划线), +(前面的内容有1个以上),?(0个或1个),*(有没有都可以) // 如果符合条件,则返回true,否则返回false if (/^\d+$/.test(pageNo)) { // 还要判断是否在范围内 if (pageNo >= 1 && pageNo <= <%=allPages%>) { // 合法 return true; } } return false; } </script> </body> </html>