读书人

Struts初始使用记录

发布时间: 2012-09-24 13:49:41 作者: rapoo

Struts初步使用记录
Struts2
依赖包在app/struts2-blank-2.2.1.war中


Web.xml 配置
<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>


Struts.xml配置

Struts.xml放在Src下



<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="test" namespace="/test" extends="struts-default">
<action name="hello"
method="hello">
<result name="success">/first.jsp</result>
</action>
</package>
</struts>

Name—可以自定义.
Namespace-自定义 与访问地址有关
Extends 默认 缺省包命

Action里
Name 映射关系.通过name的值访问 first..class
Method 访问类的方法.可以省略.
Result 与first.class的return一样

package cn.com.leadfar;

public class FirstAction {
private String name;
private int age;
private Integer num;
public String hello(){
name="zhangsan";
age=20;

System.out.println("hello!");
return "success";
}
public String say(){
System.out.println("say```````!");
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}

}

浏览器访问路径就是
http://localhost:8080/工程名/test/hello.action
如果没写method方法.
可以这样访问
http://localhost:8080/工程名/test/hello!hello.action
所访问的方法.跟在”!”后面.


通过request/session 等传输


Struts.xml中 添加


<%@ taglib prefix="s" uri="/struts-tags" %>
可通过ServletActionContext.getRequest()/getSession()等方法来获得request/session对象,然后调
用其中的setAttribute()方法来传值。

演示各种数据的传输、展现技巧!

在Action中通过request/session来传值:
public String detail(){

//通过request
ServletActionContext.getRequest().setAttribute("sex", "男");

//通过session

ServletActionContext.getRequest().getSession().setAttribute("address", "
北京");

//通过session
ActionContext.getContext().getSession().put("postcode",
"1234567");

return "detail";
}




在JSP中取值:

<body>

<!-- 从request中取sex值 -->
request.sex = <s:property value="#request.sex"/> <br/>
request.sex = <s:property value="#request['sex']"/> <br/>

<!-- 从session中取值 -->
session.address = <s:property value="#session.address"/> <br/>
session.postcode = <s:property value="#session.postcode"/> <br/>

<!-- 依次搜索page/request/session/application scope取值 -->
attr.postcode=<s:property value="#attr.postcode"/> <br/>
</body>
总结:
在Action.java中 定义变量 输入
Private String name;
ActionContext.getContext().put(“name”,”李四”);
在jsp中输出
Name: <s:property value=”#name” />

数组取值


List users = new ArrayList();
for (int i = 0; i <5; i++) {
User u =new User();--注意 集合里new的对象.
u.setName("user"+i);
u.setAge(i+20);
users.add(u);
}
ActionContext.getContext().put("users",users);

注意第二个User 没有引号


Jsp中

<s:iterator value="#users">
//注意 iterator 后面还有一个value的值需要指定
name:<s:property value="new cn.com.leadfar.Utils().toUp(name)"/><br>
name:<s:property value="name"/><br>
age:<s:property value="age"/><br>

</s:iterator>

调用方法

【注意,在最新的 struts2 版本中,要想在 JSP 中通过 OGNL 表达式访问静态方法,则必须配置如下
constant:
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>


则在JSP中可以直接通过OGNL表达式来访问这些方法:
<!-- 调用静态方法 -->
<s:property value="@cn.com.leadfar.Utils@toUpperCase(username)"/>
@包路径@方法名

<!-- 利用OGNL表达式创建Utils对象,并调用它的实例方法 --> 非静态方法调用
<s:property value="new cn.com.leadfar.Utils().toLowerCase(username)"/>
new 包路径.方法名
<!-- 调用Action对象的getUtils().toLowerCase()方法 -->
<s:property value="utils.toLowerCase(username)"/>
*定义一个getUtils方法


//************************* OGNL调用静态方法和变量 *********************//
public void testOgnl13() throws Exception{
User user = new User();
user.setUsername("王五");
//调用静态变量
//注意:out是System中的静态变量,out是PrintStream类型的一个对象
//而println()则是out这个对象中的实例方法(不是静态方法)
//调用静态方法,需要在类名和变量名前面加上@来调用,对于实例方法,用"."来调用
Ognl.getValue("@System@out.println(username)", user);
} 版本: 1.0
Struts2介绍 更新日期: 2010-08-21 17:48


Confidential ?www.leadfar.org, 2010 Page 20 of 41


public void testOgnl14() throws Exception{
User user = new User();
user.setUsername("wangwu");
//调用静态方法,注意使用全路径类名

Ognl.getValue("@System@out.println(@cn.com.leadfar.utils.Utils@toUpperCase(username)
user);
}
Ps;注意 方法后面要记得传参数;

.赋值方法与取值方法基本相同.
把getValue变成setValue
如果是context.在Value加 一个 context;

User user = new User();
Ognl.getValue("setUsername('王五')", user);
Ognl.setValue(“setUsername(‘wangwu’),user);
String value = (String)Ognl.getValue("getUsername()", user);

public void testOrg04() throws Exception{
User user=new User();
Map context =new HashMap();

Ognl.setValue("name",context, user,"zhangsan");

log(user.getName());

此处的context没有调用.可以删除.




利用OGNL访问数组、集合对象

public void testOgnl15() throws Exception{

Object root = new Object();
Map context = new HashMap();

//利用OGNL创建java.util.List对象
List list = (List)Ognl.getValue("{123,'xxx','kdjfk'}", context,
root); //String要用单引号引起来
基本形式是 “({xxx,xxx,xxx,}”,context,root);
context.put("list", list);

//利用OGNL创建数组
int[] intarray = (int[])Ognl.getValue("new int[]{23,45,67}",
context, root);
context.put("intarray", intarray);

//利用OGNL表达式创建java.util.Map对象
Map mapvalue =
(Map)Ognl.getValue("#{'listvalue':#list,'intvalue':#intarray}", context,
root);
context.put("mapvalue", mapvalue);

//利用OGNL表达式访问这些数组和集合对象
Ognl.getValue("@System@out.println(#list[1])", context,root);
Ognl.getValue("@System@out.println(#intarray[2])", context,root);
Ognl.getValue("@System@out.println(#mapvalue.listvalue[0])",
context,root);
Ognl.getValue("@System@out.println(#mapvalue['intvalue'][0])",
context,root);
}


访问Context里的对象 用#号



读书人网 >软件架构设计

热点推荐