Struts2使用
第一步:在eclipse中創建一個動態頁面項目,單擊“file”會彈出如下的菜單。
第二步:單擊“file->Dynamic Web Project”命令。即彈出如下對話框 ,並在Project name文本輸入框中輸入“struts2pro”的項目名稱。
第三步:單擊“next”按鈕,進入“Configure project for building a Java application ”。
第四步:單擊“next”按鈕,進入 “Configure web module setting” 。
第五步:在上圖中一定要選擇“Generate web.xml deployment descriptor”的複選框,然後單擊“Finish”完成項目的創建。如下圖所示 。
第六步: 導入struts2的數據包。在struts2開發,一般情況下最少導入的jar包,去apps下的struts2-blank示例程序中copy。如下圖所示。
在上圖中我們看到了一個“struts2-blank.war”文件,我們把改名爲:struts2-blank.rar 。對struts2-blank.rar進行解壓,然後進行解壓文件夾,打開apps/struts2-blank/WEB-INF/lib目錄,如圖所示。如下圖所示。
第七上:把apps/struts2-blank/WEB-INF/lib目錄中所有的jar包複製到我們項目中的 lib。
第八步:把所有的jar包添加到類庫中,即選擇所有jar包然後右擊,在彈出的菜單中選擇“build Path-Add to build Path”,完成Struts2導入數據包。如下圖所示。
第九步:創建struts2的視圖index.html。
index.html的代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>這是我第一個struts2的視圖!</h1>
</body>
</html>
第十一步:添加 struts2 的過濾器。
爲了確保struts2能在你的web應用中運行,需要在web.xml中添加過濾器設置來啓用struts2。如下圖所示:
其中在Web.xml的代碼如下。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>struts2Pro</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
注意:上面的 <url-pattern>/*</url-pattern>意味着struts2的過濾器將會被應用到這個web 應用的所有URL上
第十二步:創建 struts.xml。如果覺得寫文件有困難,可以打開原來解壓後struts2壓縮包的apps/struts2-blank/WEB-INF/src/java目錄中把struts.xml文件複製到web項目中的src目錄中。
struts.xml代碼如下。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="struts_basic" extends="struts-default">
<action name="index">
<result>/index.html</result>
</action>
</package>
</struts>
這個最簡便的配置文件告訴struts2框架,如果訪問的URL是http://localhost:8080/struts2pro/index.action 或是http://localhost:8080/struts2pro/index 則轉發到index.html視圖
Web應用中通常需要訪問的Servlet API就是HttpServletRequest、HttpSession、ServletContext這3個類,分別代表JSP內置對象中的request、session、application,操作非常方便。那麼在struts2中如何訪問常用的內置對象呢,下面我們通過以下幾個方法來完成這個操作。
一、通過ActionContext來獲取
Struts2提供了ActionContext類,通過ActionContext類訪問Servlet API
下面是ActionContext類中包含的幾個常用方法。
1、public Object get(Object key):取得HttpServletRequest中key的值;
2、void put(String key,Object value):設置HttpServletRequest中key的值爲value;
3、public Map getApplication():獲取封裝了ServletContext的Map對象;
4、void setApplication(Map application):設置ServletContext實例;
5、static ActionContext getContext():靜態方法,獲取系統的ActionContext實例;
6、Map getParameters():類似於HttpServletRequest中的getParametersMap方法;
7、public Map getSession():獲取封裝了HttpSession的Map對象;
8、void setSession(Map session):直接傳入一個Map實例,將該Map實例裏的key-value對轉換爲session的屬性名和屬性值;
實例:
package action;
import java.util.Map;
import javax.servlet.http.HttpSession;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class GetInternalObject extends ActionSupport {
@Override
public String execute() throws Exception {
String name="zhangSan";
//獲取seesion
Map session = ActionContext.getContext().getSession();
//把想存的值放到session域中
session.put("sessionName", name);
//獲取request
Map request=(Map)ActionContext.getContext().get("request");
//把想存的值存入request域中
request.put("requestName", name);
//獲取application
Map application = ActionContext.getContext().getApplication();
//把需要的值存入application域中
application.put("applicationName", name);
return null;
}
}
二、通過使用ServletActionContext類來獲取(個人推薦使用這種方法)
package action;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class GetInternalObject extends ActionSupport {
private HttpServletRequest request;
private HttpServletResponse response;
private HttpSession session;
private ServletContext application;
@Override
public String execute() throws Exception {
request = ServletActionContext.getRequest();
response=ServletActionContext.getResponse();
session = request.getSession();
application = session.getServletContext();
return null;
}
}
三、實現相關的Aware接口。
request 對應ServletRequestAware接口
response對應ServletResponseAware接口
session 對應SessionAware接口
application對應ServletContextAware接口
例如:
package action;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class GetInternalObject extends ActionSupport implements ServletRequestAware,ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse response;
private HttpSession session;
private ServletContext application;
@Override
public String execute() throws Exception {
response.getWriter().println("hello world!");
return null;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.request=arg0;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response=arg0;
}
}