标签:style io ar color 使用 sp java strong on
下面是struts.xml中的配置
<!-- 以下设置参数值可以在struts2-core.jar中的org.apache.struts下的default.properties文件中查到 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<!-- value为true时(默认值为false)是开发模式,有更改会自动提交到web服务器去 -->
<constant name="struts.devMode" value="true"/>
<!-- 该设置是允许jsp中访问静态成员 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
<!-- 引入另外一个外部xml配置文件 -->
<include file="/com/java/login/struts2/action/ognl.xml"/>
以下是ognl.xml的配置
<struts>
<package name="ognl" extends="struts-default">
<action name="ognl" class="com.java.login.struts2.action.OgnlAction">
<result>/ognl.jsp</result>
</action>
<action name="test" class="com.java.login.struts2.action.TestAction">
<!-- chain服务器端跳转 -->
<result type="chain">ognl</result>
</action>
</package>
</struts>
OGNL使用介绍:
<%@ taglib uri="/struts-tags" prefix="s" %>
<body>
<ol>
<li>访问值栈中的action的普通属性: userName= <s:property value="userName" /></li>
<li>访问值栈中对象的普通属性(get set方法): <s:property value="user.age" /></li>
<li>什么叫OGNL:Object Graph Navigation Language -对象图导航语言</li>
<li>访问值栈中对象的普通属性: <s:property value="cat.friend.name"/></li>
<li>访问值栈中对象的普通方法: <s:property value="password.length()"/></li>
<li>访问值栈中对象的普通方法: <s:property value="cat.miaomiao()"/></li>
<li>访问值栈中action的普通方法: <s:property value="method()"/></li>
<hr/>
<li>访问静态方法: <s:property value="@com.java.login.struts2.action.Test@getValue()"/></li>
<li>访问静态属性: <s:property value="@com.java.login.struts2.action.Test@STR"/></li>
<li>访问Math类的静态方法: <s:property value="@@max(2,3)"/></li>
<hr/>
<li>访问普通类的构造方法: <s:property value="new com.java.login.struts2.pojo.User()" /></li>
<hr/>
<li>访问List: <s:property value="users"/> </li>
<li>访问List中某个元素: <s:property value="users[1]"/></li>
<li>访问List中元素某个属性的集合: <s:property value="users.{age}"/></li>
<li>访问List中元素某个属性的集合中的特定值: <s:property value="users.{age}[0]"/></li>
<hr/>
<li>访问Set: <s:property value="dogs"/></li>
<li>访问Set中某个元素: <s:property value="dogs[1]"/></li>
<hr/>
<li>访问Map: <s:property value="dogMap"/></li>
<li>访问Map中某个元素: <s:property value="dogMap.dog101"/> | <s:property value="dogMap[‘dog102‘]"/> | </li>
<li>访问Map中所有的Key: <s:property value="dogMap.keys"/></li>
<li>访问Map中所有的Value: <s:property value="dogMap.values"/></li>
<li>访问容器的大小: <s:property value="dogMap.size()"/></li>
<hr>
<li>投影(过滤): <s:property value="users.{?#this.age==1}[0]"/></li>
<li>投影(^开始的那个): <s:property value="users.{^#this.age>1}.{age}"/></li>
<li>投影($结尾的那个): <s:property value="users.{$#this.age>1}.{age}"/></li>
<li>投影(判断): <s:property value="users.{$#this.age>1}.{age}==null"/></li>
<hr>
<li>[]:<s:property value="[0]"/></li>
</ol>
<s:debug></s:debug>
</body>
上边与Action交互的类:
public class OgnlAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Cat cat;
private Map<String, Dog> dogMap = new HashMap<String, Dog>();
private Set<Dog> dogs = new HashSet<Dog>();
// 成员变量
private String password;
// 实体对象
private User user;
private String userName;
private List<User> users = new ArrayList<User>();
public OgnlAction() {
users.add(new User(1));
users.add(new User(11));
users.add(new User(12));
users.add(new User(13));
dogs.add(new Dog("dog1"));
dogs.add(new Dog("dog2"));
dogs.add(new Dog("dog3"));
dogMap.put("dog100", new Dog("dog100"));
dogMap.put("dog101", new Dog("dog101"));
dogMap.put("dog102", new Dog("dog102"));
}
@Override
public String execute() throws Exception {
return "success";
}
public Cat getCat() {
return cat;
}
public Map<String, Dog> getDogMap() {
return dogMap;
}
public Set<Dog> getDogs() {
return dogs;
}
public String getPassword() {
return password;
}
public User getUser() {
return user;
}
public String getUserName() {
return userName;
}
public List<User> getUsers() {
return users;
}
public String method() {
return "Hello";
}
public void setCat(Cat cat) {
this.cat = cat;
}
public void setDogMap(Map<String, Dog> dogMap) {
this.dogMap = dogMap;
}
public void setDogs(Set<Dog> dogs) {
this.dogs = dogs;
}
public void setPassword(String password) {
this.password = password;
}
public void setUser(User user) {
this.user = user;
}
public void setUserName(String userName) {
this.userName = userName;
user.setUserName(userName);
}
public void setUsers(List<User> users) {
this.users = users;
}
}
Structs2 ognl说明
标签:style io ar color 使用 sp java strong on
原文地址:http://blog.csdn.net/love_xsq/article/details/41819601