struts2.0 新手入门

创建工程,比如Struts2
struts-2.0.6\lib 中的的 jar 文件全部(为了后面的功能扩张)粘贴工程 Struts2 lib 目录中。
二、 配置项目的 web.xml
Struts2.0 所有的配置被整合在一个 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,代码如下:
package com.filter;
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方法:
package com.bean;
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 = " 提交 " /> &nbsp;&nbsp; < input type = "reset" value = " 重置 " />< br >
</ form >
</ body >
创建com.action包,其中写MyAction.java文件,注意它是ActionSupport类的子类。ActionSupport类在 com.opensymphony.xwork2.ActionSupport 中。
package com.action;
import com.bean.Person;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport {
private Person person ;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
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>
<package name="default" 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" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<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>
其中,<s:property>标签作用是在页面上显示一个值。
同样,Struts在解析了表达式语言后,会调用getPerson().getName()方法,读出姓名的值,性别、地址、电话等都是类似的。
注:开发环境:myeclipse6.0+tomcat5.5+jdk1.5
Tomcat必须5.5以上,要不然会报filter错

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值