struts2入門

一直沒有機會接觸struts2,以前總是從同學那裏瞭解到是個很不錯的東東!所以進來就學了學!
當然,首先要創建一個web工程.然後將struts2下lib中的所有非plugin的jar包copy到當前lib下,struts2.x和struts1.x不同. struts2.x所有的配置被整合在一個Filter裏面,該Filter位於org.apache.struts2.dispatcher.FilterDispatcher,因此,在web.xml中應該這樣聲明:
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

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

但是,該Filter一個問題,就是從頁面傳到後臺的中文經過這個過濾器後會變成亂碼,爲了解決這個問題,需要重寫這個過濾器,最簡單的方法是寫一個類繼承FilterDispatcher,在src目錄下創建com.filter包,在包中建立NewFilterDispatcher類,繼承FilterDispatcher,代碼如下:

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.struts2.dispatcher.FilterDispatcher;

public class NewFilterDispatcher extends FilterDispatcher {
private static String encoding = "GB2312";

public void init(FilterConfig filterConfig) throws ServletException {
super.init(filterConfig);
String encodingParam = filterConfig.getInitParameter("encoding");
if (encodingParam != null && encodingParam.trim().length() != 0) {
encoding = encodingParam;
}
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
super.doFilter(request, response, chain);
}


}

這時web.xml中相應的地方就改爲:

<filter>
<filter-name>struts</filter-name>
<filter-class>
com.filter.NewFilterDispatcher
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
</filter>

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

說明:
(1)該類是FilterDispatcher類的子類。
(2)該類有個成員變量,名爲encoding,默認是“GB2312”。
(3)注意在web.xml中,<filter>標籤裏多了<init-param>標籤,顧名思義,它的作用是初始化一個參數,裏面定義了參數名和參數值。因此,在子類中,需要重寫init方法,其中:
String encodingParam = filterConfig.getInitParameter("encoding");
就是從web.xml中讀出了參數名爲encoding的值,然後賦給子類中的encoding成員。
(4)重寫dofilter方法,加上:
request.setCharacterEncoding(encoding);
然後再調用父類的dofilter方法,這樣就完成了編碼的轉換。
(5)如果需要使用其它編碼(如“UTF-8”等),只要改變<param-value>中的值即可。
這樣就把struts2.0加入到工程中了。

三、Struts2.0的配置文件
除了在web.xml中配置以外,struts2.0還有幾個自己的配置文件,其中最重要的兩個是struts.properties和struts.xml,都要放到src目錄下。

Struts.properties的原文件可以在struts-core-2.0.x.jar中找到,原名叫default.properties,將其解壓出來,並改名爲struts.properties,放到工程的src目錄下,然後還需要修改裏面的值。比如:
struts.locale=zh_CN
struts.i18n.encoding=GB2312
修改以後,這樣struts才能認識中文。
再比如:
struts.action.extension=action
這是個默認值,意思說,struts的每個action的後綴都是.action。
Struts.xml文件用於配置所有的action,在後文有詳細的配置方法。
四、創建JavaBean
創建com.bean包,確定需要輸入的個人信息有“姓名”、“性別”、“地址”、“電話”,因此先定義一個JavaBean,名爲Person.java,放到com.bean包中,它包括四個成員以及相應的get、set方法:

public class Person {

private String name;

private String sex;

private String address;

private String phone;

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

}


新建index.jsp,修改body
body>
<h4>請輸入你的基本信息</h4>
<form name="infoForm" action="go.action" method="POST">
姓名:<input type="text" name="person.name" /><br>
性別:<select name="person.sex">
<option value="男">男</option>
<option value="女">女</option>
</select><br>
地址:<input type="text" name="person.address" /><br>
電話:<input type="text" name="person.phone" /><br>
<input type="submit" value="提交" />  <input type="reset" value="重置" /><br>
</form>
</body>

創建com.action包,其中寫MyAction.java文件,注意它是ActionSupport類的子類。ActionSupport類在com.opensymphony.xwork2.ActionSupport中。
import com.iss.bean.Person;
import com.opensymphony.xwork2.ActionSupport;

public class MyAction extends ActionSupport {
private Person person;

@Override
public String execute() throws Exception {

return SUCCESS;
}

public Person getPerson() {
return person;
}

public void setPerson(Person person) {
if (person == null)
person = new Person();
this.person = person;
}

}

在struts.xml中聲明該action:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />
<package name="tutorial" extends="struts-default">
<action name="go" class="com.action.MyAction">
<result name="success">/next.jsp</result>
</action>
</package>
</struts>

說明:
(1)MyAction類中定義了Person類的成員對象以及它的get、set方法。
(2)MyAction類的execute方法重寫了父類的方法,其中的SUCCESS是父類中定義的常量,和struts.xml文件中<result>標籤中的success相對應。
(3)index.jsp中,表單的action屬性爲“go.action”,與struts.xml中聲明的action名字一致。
(4)注意index.jsp中各表單元素的name屬性,比如:
<input type="text" name="person.name" />
在表單提交時,struts會在MyAction類中尋找getPerson()這個方法以及“person”這個成員變量,由於person是Person類的一個對象,它本身還有一個name屬性,因此struts還會自動調用該對象的“setName”方法,給name屬性賦值,因此,在Person類的定義中各成員變量必須有get、set方法。
我們把“person.name”這樣的語句叫做表達式語言(Expression Language,簡稱爲EL)。它由XWork框架提供,XWork表達式語言的核心是OGNL(Object Graph Notation Language),OGNL是一種功能強大,技術成熟,應用廣泛的表達式語言。用戶在表單中輸入的數據,會被保存到一個叫做“值堆棧”(OgnlValueStack)的地方中去,當業務操作完成,結果數據會通過表達式被獲取、輸出。實際上,在這個例子中,Struts在解析了該表達式語言後,執行了getPerson().setName()方法,把頁面上的值賦給person對象的name屬性。
新建next.jsp,加入struts標籤庫引用

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP ''next.jsp'' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
以下是你輸入的信息:
<br>
姓名:
<s:property value="person.name" />
<br>
性別:
<s:property value="person.sex" />
<br>
地址:
<s:property value="person.address" />
<br>
電話:
<s:property value="person.phone" />
<br>
</body>
</html>


tomcat5.5或以上,jdk1.5或以上
發佈了24 篇原創文章 · 獲贊 0 · 訪問量 2623
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章