码迷,mamicode.com
首页 > 其他好文 > 详细

OJNL表达式简单案例

时间:2015-10-20 13:53:35      阅读:1030      评论:0      收藏:0      [点我收藏+]

标签:

输入页面:

技术分享
 1 <%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%>
 2 <%@ taglib uri="/struts-tags" prefix="s" %> 
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
 4 <html> 
 5   <head> 
 6     <title>添加用户</title> 
 7  <meta  http-equiv="content-type"  content="text/html;  charset=GBK"> 
 8     <style type="text/css"> 
 9     td { 
10        border: 1px solid black; 
11          } 
12     body { 
13           text-align: center; 
14       } 
15   </style>
16    <script type="text/javascript"> 
17     function validateForm(){ 
18        return true; 
19     } 
20     </script> 
21     </head>
22     
23     <h2 align="center" style="margin-top: 5px;">添加用户</h2> 
24   <hr> 
25   <body> 
26      <s:form action="userAction"  method="post" onsubmit="return validateForm()"> 
27           <table id="userList"  width="40%" height="400px" align="center"cellspacing="0" style="border-collapse:collapse; border: 1px solid black;"> 
28               <tr align="center"> 
29                   <td>用户1</td> 
30                   <!--
31                              中文乱码问题:在struts.xml中添加:
32                    <constant name="struts.i18n.encoding" value="GBK"/>             
33                     -->
34                   <td align="center"><s:textfield name="user.username" id="username" label="用户" size="35px"/></td> 
35              </tr> 
36        
37              <tr align="center"> 
38                   <td>密码</td> 
39                   <td align="left"><s:password name="user.password" id="password" label="密码"  size="35px"/></td> 
40              </tr> 
41       
42              <tr align="center"> 
43                   <td>性别</td> 
44                   <!--  
45                                这样只能输出1、0
46                   <td align="left"><s:radio name="user.sex" list="#{1:‘男‘,0:‘女‘}"/></td> 
47                               
48                   -->
49                   <td align="left"><s:radio name="user.sex" list="#{‘男‘:‘男‘,‘女‘:‘女‘}"/></td> 
50              </tr> 
51        
52              <tr align="center"> 
53                    <td>语言</td> 
54                    <td align="left">  <s:checkboxlist name="user.language" list="#{1:‘中文‘,2:‘英文‘,3:‘法文‘,4:‘德文‘}"/>  </td> 
55              </tr> 
56        
57              <tr align="center"> 
58                    <td>头像</td> 
59                    <td align="left"> <s:file name="user.file" label="上传头像"/> </td> 
60              </tr> 
61             
62               <tr align="center"> 
63                      <td>省份</td> 
64                      <td align="left">
65                           <s:select name="user.province" list="#{51:‘四川‘,52:‘云南‘,53:‘贵州‘}" cssStyle="width:100px"/> 
66                      </td> 
67               </tr>       
68               <tr align="center"> 
69                      <td>备注</td>
70                      <td align="left"><s:textarea rows="5" cols="50" name="user.remark"/></td> 
71               </tr> 
72         
73                <tr align="center"> 
74                      <td> <s:submit type="button" value="保存"/></td>
75                      <td> <s:reset value="重置"/></td></tr>
76         </table>
77      </s:form>
78      
79     
80   </body> 
81 </html> 
tag.jsp

输出页面:

技术分享
 1 <%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html; charset=GBK"%>
 2 <%@ taglib uri="/struts-tags" prefix="s" %> 
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6 
 7     <title>index.jsp</title>
 8 
 9   </head>
10   
11   <body>
12     <h3>form表单传值取值!</h3><br>
13         
14        <!-- date -->
15        <!--   Action.getContext().put("",)   format:版本号 -->
16                当前时间:<s:date name="#request.birth" format="yyyy年MM月dd日hh时mm分ss秒"/>
17        <br>
18        <!--在tag.jsp将数据保存到user.username,通过form表达submit到UserAction,通过user.getUserName获取到数据,再通过ActionContext.getContext
19           这种Map集保存,最终目标页面通过<s:property value="#session.username"/>输出值
20          -->
21                用户名:<s:property value="#session.username"/><br>
22                密码:  <s:property value="#request.password"/><br>
23                性别:<s:property value="#request.sex"/><br>
24                语言:<s:property  value="#request.languange"/><br>
25        <!-- if else --><br>
26         <h3>if else用法</h3>
27         <s:set name="age" value="25" />
28         
29         <s:if test="#age==23">
30          23
31         </s:if>
32         
33         <s:elseif test="#age==21">
34          21
35         </s:elseif>
36         
37         <s:else>
38          都不等
39         </s:else>
40         
41         
42         <br><h3>一、iterator标签的使用</h3><br>
43         <!-- 设置set集合  value-->
44           <!-- status 可选属性,该属性指定迭代时的IteratorStatus实例 -->
45           <!-- value="#attr.list"   list存放到了request中  可以value="#request.list"
46              statu.odd返回当前被迭代元素的索引是否是奇数-->
47              
48          <s:set name="list" value="{‘a‘,‘b‘,‘c‘,‘d‘}"/>         
49         <s:iterator var="ent" value="#request.list" status="sta">   
50                <s:if test="%{#sta.odd}">
51                 <font color="red">    
52                        <s:property value="#ent" />            
53                 </font>
54                </s:if>
55                 <s:else>  
56                        <s:property value="#ent" />
57                </s:else>
58         </s:iterator>
59         
60         
61         <br> <h3>二、Map集合</h3>
62          <!-- map集合的特点: 语法格式:# {key:value,key1:value1,key2:value2,.....}   
63          以上的语法中就直接生成了一个Map类型的集合,该Map对象中的每个key-value对象之间用英文的冒号隔开   ,多个元素之间用逗号分隔。   
64         -->
65        <s:set var="map" value="#{‘1‘:‘老李‘,‘2‘:‘老王‘,‘3‘:‘老张‘}"></s:set>
66            遍历Map: <br/>
67        
68        <s:iterator value="#map">
69           <s:property value="key" /> : <s:property value="value" /><Br />
70        </s:iterator>
71        
72        <h3>三、遍历集合</h3>
73         <!-- 遍历出年龄大于18岁的 -->
74         <s:iterator var="my" value="#request.myage">
75              <s:if test="#my > 18">
76                  <s:property value="#my"/>
77                 </s:if>
78         </s:iterator>
79     
80   </body>
81 </html>
index.jsp

控制代码:

技术分享
package action;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.catalina.connector.Request;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import bean.User;
//此action继承自ActionSupport
public class UserAction extends ActionSupport {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    //创建JavaBeab的对象
    public User user;
    //javaBean对象的set与get方法
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    } 
    
    
    
    
    //Action中必有一个方法
     public String execute() {
    
         //创建Date对象,通过ActionContext.getContext.put保存值
         //注意页面输出格式可以调为format="yyyy年MM月dd日hh时mm分ss秒"
        Date date = new Date();
        ActionContext.getContext().put("birth",date);
        
    //创建ArrayList数组,添加内容,保存到user中
        List<Integer> ageList =new ArrayList<Integer>();
        ageList.add(18);
        ageList.add(19);
        ageList.add(20);
        ageList.add(21);
        ageList.add(22);
        ageList.add(23);
        user.setAge(ageList);
        ActionContext.getContext().put("myage", user.getAge());
        
        
        
        
        
        
        
        
        //从页面获取user.getUsername()
        String str = user.getUsername();
        ActionContext.getContext().getSession().put("username",str);
        //从页面获取user.getPassword()
        ActionContext.getContext().put("password", user.getPassword());
        ActionContext.getContext().put("sex", user.getSex());
    
        
        /*    ActionContext.getContext().put("language", user.getLanguage());
        ActionContext.getContext().put("file", user.getFile());
        ActionContext.getContext().put("province", user.getProvince());
        ActionContext.getContext().put("remark", user.getRemark());*/


    
    for(int i=0;i<ageList.size();i++){
            System.out.println(user.getAge());    
      }
         
         
         return SUCCESS;
     }
}
UserAction.jsp

bean:

技术分享
 1 package bean;
 2 
 3 import java.io.File;
 4 import java.util.List;
 5 
 6 
 7 
 8 public class User {
 9     private String username;  
10     private String password;
11     private String sex;
12     private String language;
13     private File file;
14     private String province;
15     private String remark;
16    
17     private List<Integer> age;
18 
19     public String getUsername() {
20         return username;
21     }
22 
23     public void setUsername(String username) {
24         this.username = username;
25     }
26 
27     public String getPassword() {
28         return password;
29     }
30 
31     public void setPassword(String password) {
32         this.password = password;
33     }
34 
35     public List<Integer> getAge() {
36         return age;
37     }
38 
39     public void setAge(List<Integer> age) {
40         this.age = age;
41     }
42 
43     public String getSex() {
44         return sex;
45     }
46 
47     public void setSex(String sex) {
48         this.sex = sex;
49     }
50 
51     public String getLanguage() {
52         return language;
53     }
54 
55     public void setLanguage(String language) {
56         this.language = language;
57     }
58 
59     public File getFile() {
60         return file;
61     }
62 
63     public void setFile(File file) {
64         this.file = file;
65     }
66 
67     public String getProvince() {
68         return province;
69     }
70 
71     public void setProvince(String province) {
72         this.province = province;
73     }
74 
75     public String getRemark() {
76         return remark;
77     }
78 
79     public void setRemark(String remark) {
80         this.remark = remark;
81     }
82     
83 }
User.java

struts.xml

技术分享
1   <constant name="struts.i18n.encoding" value="GBK"/> 
2  <package name="hd" namespace="/" extends="struts-default">
3      <action name="userAction" class="action.UserAction">
4             <result name="success" >/index.jsp</result>  
5      </action>
6 </package>
struts.xml

运行结果:

技术分享

技术分享

 

需要完善的地方:

   1、复选框选中后输出选中的内容

   2、下拉选择框的显示输出

   3、头像上传后显示

   

 

OJNL表达式简单案例

标签:

原文地址:http://www.cnblogs.com/gugibv/p/4894326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!