【Struts2+Spring3+Hibernate3】SSH框架整合實現CRUD_1.1

作者: [email protected]
Blog: http://my.csdn.net/peng_hao1988
版本總覽:http://blog.csdn.net/peng_hao1988/article/details/9026897

實現步驟(在上一版本上修改)

一、①.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_3_0.xsd" version="3.0">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <description>Spring core configuration</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	</filter-class>
  	 <init-param>
  		<param-name>config</param-name>
  		<!-- 示例中將struts2的配置文件struts.xml文件放到了WEB-INF根目錄下 -->
  		<param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
	</init-param>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
    <!-- 這裏很關鍵,如果不配置,從JSP頁面就不能通過JSP標籤轉向到action -->
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>FORWARD</dispatcher>
  </filter-mapping>
</web-app>
          注意上述配置,dispatcher中添加REQUEST和FORWARD兩個配置,爲了啓用request和forward請求過濾,這樣就可以使用request轉發到action。
   ②.spring配置文件中使用properties文件配置數據源(示例的db-config.properties文件放在WEB-INF下)代碼如下:
<!-- 配置spring資源文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
<property name="locations">  
    <list>  
	<value>/WEB-INF/db-config.properties</value>  
    </list>  
</property>  
</bean> 

<!-- 數據源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="${driverClass}">
	</property>
	<property name="url" value="${url}">
	</property>
	<property name="username" value="${username}"></property>
	<property name="password" value="${password}"></property>
</bean>
    ③.db-config.properties文件的內容如下:
## database driver class
driverClass=com.mysql.jdbc.Driver
## connection url 
url=jdbc:mysql://localhost:3306/study
## user name and password for database
username=root
password=admin
## hibernate dialect
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
## create or update database
hibernate.hbm2ddl.auto=update
## whether show SQL
hibernate.show_sql=false
hibernate.format_sql=true
歡迎頁面index.jsp頁面通過轉發或重定向到login.jsp,代碼如下: 

<s:form action="userManagerAct" namespace="/" method="post" theme="simple">
	<table>
		<tr>
			<td>username</td><td><s:textfield name="user.uname"/></td>
			<td>password</td><td><s:password name="user.password"/></td>
			<td colspan="2">
				<s:submit value="submit" />
				<s:reset value="reset"/>
			</td>
		</tr>
	</table>
</s:form>
         因爲login.jsp中使用了struts標籤,所以需要重action中進入頁面,否則會報錯!如果不想通過action到頁面,可以在login.jsp中使用普通html標籤,然後用EL表達式輔助回顯。

二、從歡迎頁面index.jsp到login.jsp有多種方式,body中的代碼如下: 

<%--(1). 轉發一個HTML文件,JSP文件,或者是一個程序段. 
<jsp:forward page="/userManagerAct.action"></jsp:forward>
--%>
 
<%--(2). 通過request轉發
<% 
request.getRequestDispatcher("/userManagerAct.action").forward(request, response);
%>
--%>
<%--(3). 重定向 --%>
<% 
response.sendRedirect("userManagerAct.action");
%>
         上述三種方式中(1)、(2)爲請求轉發,(3)爲重定向,前兩種方式能成功的前提條件是web.xml文件中將request好forward配置到了struts2的filter-mapping中了,否則會報404錯誤!

三、在通過struts標籤請求action是注意:
    不要在action名稱後面添加後綴(本例中後綴爲*.action),因爲使用struts標籤會自己處理後綴,如果添加了後綴,後臺會拋出一個警告

No configuration found for the specified action: 'xxxx.action' in namespace: '/'. Form action defaulting to 'action' attribute's literal value.

        網上一部分資料中說上述警告是由於沒有添加namespace導致的,但是本人測試時發現不給action添加後綴,無論有無namespace都沒有發現警告,若添加上後綴就會出現警告。具體什麼原因就不詳究了,大家在開發是注意不要給struts2標籤請求的action名稱後加後綴,form中最好添加上namespace。非struts2標籤的請求(普通的html標籤、js、以及如index.jsp中)必須添加後綴(如果在web.xml中配置了後綴的話)。

四、攔截器(Interceptor)配置,實現用戶登錄狀態驗證,操作權限驗證。如示例中登錄狀態驗證和刪除權限驗證配置如下:

<package name="myApp" extends="struts-default">
	<interceptors>
		<interceptor name="loginInterceptor" class="com.hzboy.common.LoginInterceptor"/>
		<interceptor name="delInterceptor" class="com.hzboy.common.DelInterceptor"/>
		<!-- 自定義攔截器棧,擴展原有攔截器特性 -->
		<interceptor-stack name="MyInterceptors">
			<interceptor-ref name="defaultStack"/>
			<interceptor-ref name="loginInterceptor"/>
		</interceptor-stack>			
		<!-- 定義刪除信息攔截器棧 -->
		<interceptor-stack name="delInterceptors">
			<interceptor-ref name="MyInterceptors"/>
			<interceptor-ref name="delInterceptor"/>
		</interceptor-stack>
		
	</interceptors>
	<!-- 修改默認攔截器,在默認攔截器的基礎之上添加了登錄驗證 -->
	<default-interceptor-ref name="MyInterceptors"/>
	
	<global-results>
		<result name="login">/login.jsp</result>
		<result name="query">/WEB-INF/jsp/success.jsp</result>
	</global-results>
	
	<action name="userManagerAct" class="userAct" method="doLogin">
		<!-- 因爲修改了默認攔截器,添加了登錄驗證,所以不能再使用默認的攔截器,登錄請求不需要驗證,故使用struts默認的攔截器 -->
		<interceptor-ref name="defaultStack"/>
	</action>
	
	<action name="query" class="userAct" method="doQuery" />
	
	<action name="delete" class="userAct" method="doDelete">
		<interceptor-ref name="delInterceptors"/>
		<result name="delMsg">/WEB-INF/jsp/message.jsp</result>
	</action>
	
	<action name="edit" class="userAct" method="doEdit">
		<result name="editUser">/WEB-INF/jsp/editUser.jsp</result>
	</action>
	
	<action name="add" class="userAct" method="doAdd">
		<result name="addUser">/WEB-INF/jsp/addUser.jsp</result>
	</action>
</package>
         ①. 自定義攔截器:在package中定義,並且可以通過package的繼承來被其他的子package引用,可以將多個攔截器在interceptor-stack中引用組成一個攔截器棧,攔截器棧會按照引用的順序依次執行。action中添加攔截器或攔截器棧使用interceptor-ref來實現。如果想讓某個攔截器或攔截器棧作爲默認攔截器(即替換原有的defaultStack)可以使用default-interceptor-ref來指定。默認的攔截器會攔截前包和子包中的所有action請求,所以除非當前包下所有action都需要某個攔截操作,否則沒有必要指定default-interceptor-ref、如果指定一定要,那應該指定一個攔截器棧,而且該棧中引用了defaultStack。
注意:當action應用一個攔截或攔截器棧(未引用defaultStack)時,struts2默認的攔截器就會失效,會導致部分功能無法使用,如struts2標籤的回顯。
        ②.定義攔截器:定義一個java類繼承com.opensymphony.xwork2.interceptor.AbstractInterceptor類並實現其intercept方法,以LoginInterceptor爲例,代碼如下:
public class LoginInterceptor extends AbstractInterceptor{

	private static final long serialVersionUID = 1L;
	
	private Logger logger = Logger.getLogger(LoginInterceptor.class);
	
	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		logger.info("LoginInterceptor is runing....");
		Object user = ai.getInvocationContext().getSession().get(Global.SESSION_USER_INFO);
		if(user != null)
			return ai.invoke();
		else
			return Global.RESULT_LOGIN;
	}

}
         通過參數ActionInvocation類的invoke()方法返回到控制器繼續執行後面的攔截器,如果沒有就直接轉發到請求的action,可以返回一個字符串到指定頁面或action,該字符串應該是被攔截的action中的result的name值或是global-results中某個result的name值,否則就報404。詳細的struts2攔截器配置及介紹參見網友博文:http://blog.csdn.net/qjyong/article/details/1824607 。

五、屬性驗證、Validate
         ①.通常從web頁面提交到action的數據需要做一些有效性驗證,常見的就是在action中直接驗證屬性值,如下實例中的添加用戶代碼:

	/**
	 * @description 人員信息添加
	 * @return
	 * @author porter
	 * @created 2013-6-2 下午6:13:56
	 */
	public String doAdd(){
		logger.info("doAdd is runing...");
		String result = Global.RESULT_QUERY;
		try {
			String param = (String)this.requestGetParam("param");
			if(param != null && Integer.parseInt(param) > 0){
				if(validatation()){
					this.user.setId(0);
					userService.addUser(this.user);
					result = doQuery();
				}else
					result = Global.RESULT_ADD_USER;
			}else
				result = Global.RESULT_ADD_USER;
		} catch (Exception e) {
			logger.error(e);
		}
		return result;
	}
	
	/**
	 * @description 驗證添加的人員信息
	 * @return
	 * @author porter
	 * @created 2013-6-2 下午6:14:24
	 */
	public boolean validatation() {
		if(this.user.getUname() == null || this.user.getUname().isEmpty()){
			this.addFieldError("user.uname", "Username cant not be null!");
			return false;
		}else if(this.user.getPassword() == null || this.user.getPassword().isEmpty()){
			this.addFieldError("user.uname", "password cant not be null!");
			return false;
		}
		return true;
	}

        添加用戶信息是用戶名和密碼不能爲空,如果不符合要就就踢回到web頁面並提示用戶,提示信息可以使用addActionError,addFieldError,addActionMessage。 三種消息的用法介紹(引用
     ②.還有一種驗證方式:使用xml描述來驗證action中的屬性即validate框架。該框架需要額外添加xml配置,比較麻煩,實際開發中使用較少,詳細可參見網友博文

六、struts-json,通過ajax和json-plugin來實現數據的後臺驗證,如實例中添加用戶信息界面,當輸入完成用戶名後使用ajax去有後臺驗證用戶名是否以被佔用。實例代碼:

<!-- 配置JSON響應方法 -->
<package name="my-json" extends="json-default">
	<action name="checkUName" class="userAct" method="checkUName">
		<result type="json"  name="success">
			<!-- 只返回user.name的值,沒有key -->
			<param name="root">user.uname</param>
			
			<!-- 需要去除的屬性
			<param name="excludeProperties">userService,users,searchText</param>
			 -->
			<!-- 需要包含的屬性 
			<param name="includeProperties">user\.uname</param>
			-->
		</result>
	</action>
</package>
           result中type值的取值範圍和ajax數據格式基本一致,有json,xml、text等,name值可以隨意,但是action執行完後返回的結果必須和這裏的name值匹配。param參數有一下幾種:
          1.root參數:從返回結果中根據ognl表達式取出你需要輸出的結果
          2.excludeNullProperties 參數:表示是否去掉空值, 默認值是false,如果設置爲true會自動將爲空的值過濾,只輸出不爲空的值。
          3.ignoreHierarchy 參數:表示是否忽略等級,也就是繼承關係,比如:TestAction繼承於BaseAction,那麼TestAction中返回的json字符串默認是不會包含父類BaseAction的屬性值,ignoreHierarchy值默認爲true,設置爲false後會將父類和子類的屬性一起返回
          4.includeProperties 參數:輸出結果中需要包含的屬性值,這裏正則表達式和屬性名匹配,可以用“,”分割填充多個正則表達式。
          5.excludeProperties 參數:輸出結果需要剔除的屬性值,也支持正則表達式匹配屬性名,可以用“,”分割填充多個正則表達式。      
          頁面js中的代碼如下:
$(function(){
	//給用戶名輸入框設置焦點事件,當失去焦點是觸發事件,判斷用戶輸入的用戶名是否合法!
	$("input[name='user.uname']").blur(function(){
		var uname = $(this).val();
		if(uname == ""){
			$("#chkMsg").css({"color":"red"});
			$("#chkMsg").text("用戶名不能爲空!");
			return;
		}
		$.ajax({
			type:"post",
			url:"checkUName.action",
			data:{"uname":uname},
			dataType:"json",
			success:function(data){
				if(data == null){
					$("#chkMsg").css({"color":"#00cc00"});
					$("#chkMsg").text("該用戶名可以使用!");
				}else{
					$("#chkMsg").css({"color":"red"});
					$("#chkMsg").text("該用戶名已被使用!");
				}
			}
		});
	});
});
        注意:使用struts-json來完成數據交換需要添加struts2-json-plugin-2.x,x.jar到工程的classpath中。

示例代碼:http://download.csdn.net/detail/peng_hao1988/5542325


》》》》》》》》》》》》轉載請註明出處《《《《《《《《《《《《



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章