Spring整合Struts2
來源:程序員人生 發布時間:2014-12-17 08:52:31 閱讀次數:2500次
在之前的SpringDemo中,已將Spring和Hibernate整合到了1起,現在將Struts整合到SpringDemo中,這樣就是1個完全的基于SSH的簡單項目了。
在全部項目的構建進程,我基本是由內向外――先導入jar包、整理配置文件,再完成持久層和控制層的接口和類,最后建立表現層的jsp頁面。這樣的好處是:每完成1步即可以測1下,及時修改毛病;繼續下1層的時候不用再操心前面的內容。
1、導入strut2的jar包:
commons-logging⑴.0.4.jar----------Jakarta的通用日志記錄包
reemarker⑵.3.8.jar--------------------FreeMarker是1個模板引擎,1個基于模板生成文本輸出的通用工具
ognl⑵.6.11.jar--------------------------支持ognl表達式
struts2-core⑵.0.12.jar----------------struts2的核心包
xwork⑵.06.jar--------------------------xwork的包由于Struts2是由xwork的延伸,有些類仍然關聯著 xwork的類
commons-fileupload⑴.2.1.jar ------- struts的上傳下載
(ognl是1種功能強大的表達式,通過它簡單1致的表達式語法,可以存取對象的任意屬性,調用對象的方法,遍歷全部對象的結構圖,實現字段類型轉化等功能。它使用相同的表達式去存取對象的屬性。)
添加struts2-spring整合的插件:struts2-spring-plugin⑵.0.12.jar
Struts2與Spring的集成要用到Spring插件包struts2-spring-plugin-x-x-x.jar是同Struts21起發布的。
Spring插件是通過覆蓋(override)Struts2的ObjectFactory來增強核心框架對象的創建。當創建1個對象的時候,它會用Struts2配置文件中的class屬性去和Spring配置文件中的id屬性進行關聯,如果能找到,則由 Spring創建,否則由Struts2框架本身創建,然后由Spring來裝配。
如果不使用這個插件,則需要在struts.xml里面配置:
Xml代碼以下:
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.objectFactory.spring.autoWire" value="name" />
struts2-spring插件具體有以下幾個作用:
1.允許Spring創建Action、Interceptror和Result。
2. 由Struts創建的對象能夠被Spring裝配。
3.如果沒有使用Spring ObjectFactory,提供了2個攔截器來自動裝配action。
2、在web.xml里面配置struts2用到的核心過濾器。
<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>
3、在src目錄下新建struts.xml文件來配置。
<?xml version="1.0" encoding="UTF⑻"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts⑵.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
<!-- struts2的action必須放在1個指定的包空間下定義 -->
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="loginAction" method="execute">
<result name="success">/WEB-INF/success.jsp</result>
<result name="login">/WEB-INF/login.jsp</result>
</action>
</package>
</struts>
4、建立LoginAction,采取@Autowried自動注入AccountService。
LoginAction.java代碼以下:
package com.demo.action;
import org.springframework.beans.factory.annotation.Autowired;
import com.demo.service.AccountService;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = ⑴438866827399732426L;
@Autowired
private AccountService accountService;
private String username;
private String password;
@Override
public String execute(){
System.out.println("run execute...");
if(accountService.hasMatchAccount(username, password)){
return SUCCESS;
}
return LOGIN;
}
public String getUsername(){
return username;
}
public void setUsername(String name){
this.username = name;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
}
5、修改login.jsp,添加success.jsp
login.jsp內容以下:
<%@ page language="java" contentType="text/html; charset=UTF⑻"
pageEncoding="UTF⑻"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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⑻">
<title>Insert title here</title>
</head>
<body>
<s:form action="login" method="post">
<s:label value="系統登陸"></s:label>
<s:textfield name="username" label="賬號" />
<s:password name="password" label="密碼" />
<s:submit value="登錄" />
</s:form>
</body>
</html>
success.jsp內容以下:
<%@ page language="java" contentType="text/html; charset=UTF⑻"
pageEncoding="UTF⑻"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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⑻">
<title>Insert title here</title>
</head>
<body>
<h2>歡迎<s:property value="username"/>!</h2>
</body>
</html>
6、這樣我們就完成了對Struts2的簡單整合,完成了1個簡單的基于SSH框架的登陸Demo。對SSH有了1個基本的了解,接下來將進行更深入的學習。
由于在跟老師做項目的時候用過Spring,感覺還不錯;因此在學習的進程中主要以Spring為核心,來拓展對其他框架的學習。
每一個框架都有其豐富的功能,任何1篇文章、1部書不可能窮盡其特性。需要在在實踐進程中,邊學邊用,逐步熟習。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈