Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

Struts标签与OGNL表达式语言实例演练

$
0
0

项目结构如图:

主要类文件如下:

     AdminAction.java

public class AdminAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Admin admin;
	
	
	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public String Login(){
//        使用request存值
		ActionContext actionContext = ActionContext.getContext();
		actionContext.put("Lili", "你好!");
//		 使用session存值
		  actionContext.getSession().put("Jenny", "你好Jenny");
//		 使用application存值
		  actionContext.getApplication().put("Nike", "谢谢Nike");
//		 存入对象
//        使用request存值
		 actionContext.put("admin", admin);
//		 使用session存值
		  actionContext.getSession().put("admin", admin);
//		 使用application存值
		  actionContext.getApplication().put("admin", admin);
		  
		return "adminaction_login";
	}

	public String save(){
         System.out.println("保存成功!!!!!!");	
		
		return "adminaction_save";
	}
	public String delete(){
		System.out.println("删除成功!!!!!!");	
		
		return "adminaction_delete";
	}
	public String update(){
		System.out.println("修改成功!!!!!!");	
		
		return "adminaction_update";
	}
	public String find(){
		System.out.println("查询成功!!!!!!");	
		
		return "adminaction_find";
	}
	

}


Admin.java

public class Admin {
  private String userName;
  private String userPassword;
public String getUserName() {
	return userName;
}
public void setUserName(String userName) {
	this.userName = userName;
}
public String getUserPassword() {
	return userPassword;
}
public void setUserPassword(String userPassword) {
	this.userPassword = userPassword;
}
  
  
}
 
struts.xml
	<struts>
	    <package name="struts" namespace="/csdn" extends="struts-default" >
	        <!-- 
	         使用通配符进行测试!
	         -->
	         <action name="*_*" class="www.csdn.net.action.{1}" method="{2}">     
	         <result name="adminaction_loginError" type="dispatcher">/manager/ErrorNormal.jsp </result>
	         <result name="adminaction_login" type="dispatcher">/manager/Login.jsp </result>
	         <result name="adminaction_save" type="dispatcher">/manager/Save.jsp </result>
	         <result name="adminaction_delete" type="dispatcher">/manager/Delete.jsp </result>
	         <result name="adminaction_update" type="dispatcher">/manager/Update.jsp </result>
	         <result name="adminaction_find" type="dispatcher">/manager/Find.jsp </result>
	       </action>
	    </package>
	</struts>



index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
  <body style="text-align: center">    
  <h2>这是第一个使用structs2标签开发的应用测试实例!</h2>
  
    <s:form action="AdminAction_Login" namespace="/csdn" method="post">
      <s:textfield name="admin.userName" label="用户名:"></s:textfield>
      <s:password name="admin.userPassword" label="密码:"></s:password>
      <s:submit  value="登陆"></s:submit> 
      <s:reset value="重置"></s:reset>
     
    </s:form>
  </body>
</html>

主要实例演示:

    Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Login.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">
   <style type="text/css">
   .even{
      background-color:red;
    }
.odd{
      background-color:blue;
    }
   </style>

    
  </head>
  
  <body style="text-align: center">
  <<<<<---------------------------------------------------------------->>>>>
  <h2>这是第一个使用structs2标签和设置session属性对象开发的应用测试实例!</h2>
  <h3>${admin.userName}用户登陆成功!!!</h3><br/>
  <hr/>
  <h3>使用ongl标签输出存入不同作用域中的值:</h3>
  输出request作用域中存入的信息:
         <s:property value = "#request.Lili"/>
         <br/>
   输出sesion作用域中存入的信息:
         <s:property value = "#session.Jenny"/>
         <br/>
    输出application作用域中存入的信息:
         <s:property value = "#application.Nike"/>
          <s:debug></s:debug>
         <hr/>
         <h3>使用ongl标签输出存入不同作用域中的对象值:</h3>
           输出request作用域中存入的信息:
         <s:property value = "#admin.userName"/>
         <br/>
          输出sesion作用域中存入的信息:
         <s:property value = "#admin.userName"/>
         <br/>
          输出application作用域中存入的信息:
         <s:property value = "#admin.userName"/>
         <s:debug></s:debug>
         <hr/>
         <h3>使用struts标签构建Map</h3>
         <s:iterator var="map" value="#{1:'lihuihui',2:'李晖晖'}">
               <s:property value="key"/>
             <s:property value="value"/>
               <br/> 
            </s:iterator>
            <hr/>
           <s:iterator var="map" value="#{1:'lihuihui',2:'李晖晖'}">
           
               <s:property value="#map.key"/>
             <s:property value="#map.value"/>
              <br/>
            </s:iterator>
           
            <hr/>
              <h3>使用struts标签构建List</h3>
              方式一:<br/>
               <s:iterator var="list" value="{'Java','.NET','PHP'}">
               <s:property value="list"/>
              <br/>
            </s:iterator>
          方式二:<br/>
               <s:iterator var="list" value="{'Java','.NET','PHP','3G'}">
               <s:property />
              <br/>
            </s:iterator>
            <hr/>
          <h3>使用struts标签构建单选按钮</h3>
          方式一:<br/>
            <s:radio list="#{'m':'男','w': '女'}" listKey="key" listValue="value"></s:radio><br/>
             键值互换:
              方式二:<br/>
             <s:radio list="#{'m':'男','w': '女'}" listKey="value" listValue="key"></s:radio>
             <br/>
             方式三:<br/>
            
             <s:radio list="#{'1':'男','0': '女'}" value ="'0'"></s:radio>
            <br/>
             方式四:<br/>
            
             <s:radio list="{'男', '女'}"  value ="'男'"></s:radio>
            <br/>
                <hr/>
          <h3>使用struts标签构建复选框</h3>
            方式一:
          <s:checkbox name="fav" label="看书"/>
           <s:checkbox name="fav" label="写字"/> 
           <s:checkbox name="fav" label="画画"/>
            <s:checkbox name="fav" label="打球"/>
            <br/>
             方式二:
             <s:checkboxlist name = "fav" list="{'看书','写字','画画','打球'}"  value ="'写字','画画'"></s:checkboxlist>
           <br/>
            方式三:
            <s:checkboxlist  name ="fav" list="#{1:'看书',2:'写字',3:'打球'}" value="{1,2}"></s:checkboxlist>
            <br/>
            <hr/>
          <h3>使用struts标签构建下拉列表框</h3>
          方式一:<br/>
          <s:select label="月份" list="#{1:'Jan',2:'Feb',3:'Mar',4:'Apr'}" headerValue="选择月份" headerKey="-1"  required="true"></s:select>
          <br/>
           方式二:<br/>
           <s:select label="月份" list="#{1:'Jan',2:'Feb',3:'Mar',4:'Apr'}" headerValue="选择月份" headerKey="-1"  value="'4'" required="false"></s:select>
             <br/>
           方式三:<br/>
           <s:select label="专业" list="{'Java','.NET','3G','PHP'}" headerValue="选择专业" headerKey="-1"  value="'PHP'" required="false"></s:select>
            方式四:<br/>
           <s:select label="专业" list="{'Java','.NET','3G','PHP'}"   value="'PHP'" ></s:select>
             <hr/>
             <h3>使用struts标签构建if/else/in/not in</h3>      
              <s:if test="'Java' not in {'3G','Java'}">
                  Java is not in list!
              </s:if>
              <s:else>   Java is in list!</s:else>
              <hr/>
            <h3>使用struts标签中的set对属性进行存值</h3>
            <s:set var="user" value="你好用户!<br/>" scope="request"></s:set>
              <s:property value="#user" escape="false"/>
               <hr/>
            <h3>使用struts标签中的push对属性进行存值</h3>
              <s:push value="'lihuihui'">
              <s:property/>
              </s:push>
                 <hr/>
            <h3>使用struts标签中的bean对实体进行赋值并进行存取值</h3>
              <s:bean name="www.csdn.net.domain.Admin"  var = "myAdmin">
               <s:param name="userName">李晖晖</s:param>
                <s:param name="userPassword">12345</s:param>
                输出姓名:
                  <s:property value="userName"/>
               
              </s:bean>
              <br/>
              输出1:<s:property value="#myAdmin.userName"/>
             <br/>
              输出2:
              <s:set var="admin" value = "#myAdmin"></s:set>
              <s:property value="#admin.userName"/>
              <hr/>
         <h3>使用struts标签中的迭代实现表格的变色输出!</h3>
         <table border = "1px">
            <s:iterator var = "tb" value ="{'Java','.NET','PHP','3G'}"  status="st">
              <tr class = "<s:property value = '#st.even?"even":"odd"'/>">
                <td>
                  <s:property value ="tb"/>
                </td>
              </tr>
            </s:iterator>
         </table>
              
  </body>
</html>


效果如图:


作者:lijizh1013 发表于2013-3-8 0:36:56 原文链接
阅读:95 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>