软件编程
位置:首页>> 软件编程>> java编程>> Struts2 $,#,%详解及实例代码

Struts2 $,#,%详解及实例代码

作者:lqh  发布时间:2021-09-10 04:18:40 

标签:Struts2,$,#,%

最近在学ssh,一直搞不懂$,%,#的区别,做了点小练习,慢慢也懂了一点,将自己所学的也记录下来吧。

   存在一下一个实体entity:


public class Person {

private int id ;
 private String Name ;

public int getId() {
   return id;
 }

public Person(int id, String name) {
   super();
   this.id = id;
   Name = name;
 }

public Person() {
   super();
 }
 public void setId(int id) {
   this.id = id;
 }
 public String getName() {
   return Name;
 }
 public void setName(String name) {
   Name = name;
 }

}

在struts2的Action中,写了如下代码:


@Override
 public String execute() throws Exception {

//application  
   Person p = new Person(1,"zhangsan") ;
   ActionContext.getContext().getApplication().put("person", p);

//session
   Person p1 = new Person(3,"wangwu");
   ActionContext.getContext().getSession().put("person", p1);

//request
   Person p2 = new Person(2,"lisi");
   ActionContext.getContext().put("person", p2) ;

//servletContext
   Person p3 = new Person(5,"xiaoming");
   ActionContext.getContext().getContextMap().put("person", p3);

Person p4 = new Person(3,"wangwu");
   ActionContext.getContext().getValueStack().push(p4);

return "success";
 }

分别在application,session,request,servletContext,valueStack中存入一个person对象,那么在JSP中我们可以按照一下方式获取:


person: <input type="text" name="name" value="${person }" /><br />  
id: <input type="text" name="name" value="${person.id }" /><br />  
name: <input type="text" name="name" value="${person.name }" /><br />
<hr>

         以上代码所得出的person信息时xiaoming的,即ActionContext.getContext().getContextMap()中存放的信息,通过查询$的用法,发现$获取对象的方式是有方式的,即

ActionContext.getContext().getContextMap() > ActionContext.getContext() >ActionContext.getContext().getSession() >ActionContext.getContext().getApplication(),对于不同的scope(范围)中存在同名对象时,$的查找方式将会按照以上步骤进行,找到即输出,没有找到继续上一级查找,到顶不存在时将输出null。

   那么$的用法为:${scope.object.attribute}

   scope的属性值为request,session,application,默认不写时将按照上述所说的方案查找,找到即输出相关属性值。

  在struts标签中,存一个这样的:

<s:property value="#application.person"/>

   可以看出,此时用到了#号,个人认为,其实#和$的用法完全是一样的,只要你将需要输出的对象装进不同范围的map(servletContext,request,session和application),在view中展示时,使用<s:property value="#scope.object.attribute">跟$理解完全是一样的。但是你在使用struts的标签时,比如:


<s:textfield name="person.name"></s:textfield>

完全可以理解为


<input type="text" name="persom.name" id="person.name" value="<s:property value="#person.name"/>" />

即struts的标签已经在HTML的text中给我们封装了<s:property value="#target.name"/>,可以给我省去很多代码的。
同理,那么#的用法为:<s:property value="#scope.object.attribute" />

当然完全可以使用struts2给我们定义的标签,这样完全可以省去写过多重复代码的麻烦。其实#还有其他的用法,比如用来构造map等对象,但是个人觉得在view中写过多代码的时代已经过去,这种用法已经没有太多的意义,况且这次我只写出在view展示的过程,因此其它地方不扯了。

最后,扯一点%的用法,简单的看,%{}就是字符串计算表达式,举个例子,view中存在某个环节,一般都存在CRUD等基本功能,对于add和uppdate功能,完全可以在同一个页面完成,不同的是我们提交的地址是不同的,比如可能只这样的:对于add方法,地址为user_add.action,对于udpate方法,地址为user_update.action,那么在form中,可以使用%进行判断:


<s:form action="user_%{ id == 0 ? 'add' : 'update' }"></form>

呵呵,这样以前的两个页面现在完全一个页面可以解决掉。

同理,%与struts中的if,ifelse等判断标签联合起来用得比较多,毕竟是比较的吗。。。。


<s:if test="%{false}">
 <div>Will Not Be Executed</div>
</s:if>
<s:elseif test="%{true}">
 <div>Will Be Executed</div>
</s:elseif>
<s:else>
 <div>Will Not Be Executed</div>
</s:else>

最后,说说这个%很有用的做法,假设存在一个列表展示student全部及格的成绩(即不及格的成绩将不会展示在上面),如果使用的%将是非常简单的。不扯,先上代码:


public class Stduent implements java.io.Serializable{

private static final long serialVersionUID = -691038814755396419L;
 private int id ;
 private String name ;
 private int score ;
 private String subject ;

public int getId() {
   return id;
 }
 public void setId(int id) {
   this.id = id;
 }
 public String getName() {
   return name;
 }
 public void setName(String name) {
   this.name = name;
 }
 public int getScore() {
   return score;
 }
 public void setScore(int score) {
   this.score = score;
 }
 public String getSubject() {
   return subject;
 }
 public void setSubject(String subject) {
   this.subject = subject;
 }

/**
  * 此处判断成绩是否及格
  * @param socre
  * @return
  */
 public boolean isPast(int socre){
   return getScore() > 60 ;
 }

}

那么,现在数据库中查找学生成绩,放到list中暂时存放起来,在JSP页面,我们可以使用以下代码来控成绩制输出是否及格:


<s:iterator value="#allUser">
 <!-- 判断是否过线,过线即输出,否则舍去! -->
   <s:if test="#session.user.isPast(score)">
       name: <s:textfield name="name"></s:textfield>
       score: <s:textfield name="score"></s:textfield>\
       subject:<s:textfield name="subject"></s:textfield>
   </s:if>
</s:iterator>

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

来源:http://blog.csdn.net/u013762572/article/details/44540035

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com