favor 发表于 2013-2-6 11:11:15

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;@Overridepublic 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或以上
页: [1]
查看完整版本: struts2入门