Struts1標籤實例

首先建兩個實體類


Clazz.java
package com.model;

/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 13:35:42
* To change this template use File | Settings | File Templates.
*/

public class Clazz {
        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        private String name;
}

Student.java
package com.model;

/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 13:36:13
* To change this template use File | Settings | File Templates.
*/

public class Student {
        private String name;
        private String age;

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getAge() {
                return age;
        }

        public void setAge(String age) {
                this.age = age;
        }

        public Clazz getClazz() {
                return clazz;
        }

        public void setClazz(Clazz clazz) {
                this.clazz = clazz;
        }

        private Clazz clazz;
}

struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC
                "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
                "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>

        
                <action path="/beanWrite" type="com.tag.BeanWriteAction">
                        <forward name="success" path="/tag_beanwrite.jsp"></forward>
                </action>

                <action path="/logic" type="com.tag.LogicAction">
                        <forward name="success" path="/tag_logic.jsp"></forward>
                </action>

                <action path="/iterator" type="com.tag.IteratorAction">
                        <forward name="success" path="/tag_iterator.jsp"></forward>
                </action>
        </action-mappings>

</struts-config>

先看一下beanwrite標籤的使用
BeanWriteAction.java
package com.tag;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

import com.model.Clazz;
import com.model.Student;

/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 13:30:29
* To change this template use File | Settings | File Templates.
*/

public class BeanWriteAction extends Action {
        @Override
        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
                request.setAttribute("name","dog");
                request.setAttribute("nameHtml","<font color='red'>cat</font>");
                request.setAttribute("date",new Date());
                request.setAttribute("decimal",1234567.123);
                Clazz c=new Clazz();
                c.setName("clazzName");
                Student s=new Student();
                s.setName("張三");
                s.setAge("24");
                s.setClazz(c);
                request.setAttribute("student",s);

                return mapping.findForward("success");
                
        }
}
對應的JSP頁面是tag_beanwrite.jsp
<%--
    Created by IntelliJ IDEA.
    User: wangchenyang
    Date: 2010-11-30
    Time: 13:40:17
    To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>
<html>
    <head><title>BeanWrite示例</title></head>
    <body>
     <h1>BeanWrite標籤測試</h1>
    <bean:write name="name"></bean:write><br>
    <bean:write name="nameHtml" filter="false"></bean:write><br>
    <%--<bean:write name="date"></bean:write><br>--%> <!-- 不能直接解析-->
    <bean:write name="date" format="yyyy-MM-dd"></bean:write><br>
    <%--<bean:write name="decimal"></bean:write><br>--%> <!-- 不能直接解析-->
    <bean:write name="decimal" format="###,###.###"></bean:write><br>
    <bean:write name="decimal" format="###,###.0000"></bean:write><br><!-- 0的個數是對應的-->
    <bean:write name="student" property="name"></bean:write><br>
    <bean:write name="student" property="age"></bean:write><br> <!-- 只能顯示字符串    ?!!?實體類中int類型值的設置要考慮這個問題-->
    <bean:write name="student" property="clazz.name"></bean:write>    
    </body>
</html>

下面是Iterator標籤的使用
IteratorAction.java
package com.tag;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.Student;
import com.model.Clazz;

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

/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 16:46:17
* To change this template use File | Settings | File Templates.
*/

public class IteratorAction extends Action {
        @Override
        public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

                List<Student> list=new ArrayList<Student>();
                Clazz c=new Clazz();
                //Student student=new Student(); 如果放這的話循環只能得到最後一次循環的值(最後一次把以前的值覆蓋了)
                c.setName("clazzName");
                for(int i=0;i<5;i++){
                        Student student=new Student();
                        student.setName("student"+i);
                        student.setAge("2"+i);
                        student.setClazz(c);
                        list.add(student);
                }
                httpServletRequest.setAttribute("list",list);
                return actionMapping.findForward("success");
        }
}
對應的JSP頁面tag_iterator.jsp
<%--
    Created by IntelliJ IDEA.
    User: wangchenyang
    Date: 2010-11-30
    Time: 16:51:05
    To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>
<%@taglib prefix="bean"    uri="http://struts.apache.org/tags-bean" %>
<html>
    <head><title>Iterator標籤示例</title></head>
    <body>
    <h1>Iterator標籤示例</h1>
        <logic:empty name="list">
                沒有可以輸出的信息
        </logic:empty>
    <table border="1">
            <tr>
                    <td>姓名</td>
                    <td>年齡</td>
                    <td>班級</td>
            </tr>
             <logic:notEmpty name="list">
                <logic:iterate id="li" name="list">    
             <tr>
                     <td><bean:write name="li" property="name"></bean:write></td> <!--property 後面只跟已經定義的屬性值,不用li.name-->
                     <td><bean:write name="li" property="age"></bean:write></td>
                     <td><bean:write name="li" property="clazz.name"></bean:write></td>
             </tr>
                     </logic:iterate>
            </logic:notEmpty>
    </table>
    </body>
</html>

最後是Logic標籤的使用
LogicAction.java
package com.tag;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 16:18:49
* To change this template use File | Settings | File Templates.
*/

public class LogicAction extends Action {
        @Override
        public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
                 request.setAttribute("a",null);
                 request.setAttribute("b","");
                 request.setAttribute("c",new ArrayList());

                return mapping.findForward("success");
                
        }
}
對應的JSP頁面是tag_logic.jsp
<%--
    Created by IntelliJ IDEA.
    User: wangchenyang
    Date: 2010-11-30
    Time: 16:30:11
    To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>
<html>
    <head><title>Logic示例</title></head>
    <body>
    <h1>Logic標籤測試</h1>
    <!--當一個屬性值是null時,判斷結果爲empty和not present-->
    <logic:empty name="a">a is empty </logic:empty><br>
    <logic:notEmpty name="a">a is not empty</logic:notEmpty><br>
    <logic:present name="a">a is present</logic:present><br>
    <logic:notPresent name="a">a is not present</logic:notPresent><br>

    <!--當一個屬性值是“”(空字符串)時,判斷結果是empty和present-->
    <logic:empty name="b">b is empty</logic:empty><br>
    <logic:notEmpty name="b">b is not empty</logic:notEmpty><br>
    <logic:present name="b">b is present</logic:present><br>
    <logic:notPresent name="b">b is not present</logic:notPresent><br>

    <!--當一個屬性值是空集合時,判斷結果是empty和present-->
    <logic:empty name="c">c is    empty</logic:empty><br>
    <logic:notEmpty name="c">c is not empty</logic:notEmpty><br>
    <logic:present name="c">c is present</logic:present><br>
    <logic:notPresent name="c">c is not present</logic:notPresent><br>

    <!--當在任何範圍內都未設置該屬性時,判斷結果是empty和not present-->
    <logic:empty name="d">d is    empty</logic:empty><br>
    <logic:notEmpty name="d">d is not empty</logic:notEmpty><br>
    <logic:present name="d">d is present</logic:present><br>
    <logic:notPresent name="d">d is not present</logic:notPresent><br>


     <%--當屬性爲null時,<logic:empty>是爲空的<logic:notPresent> 也是不存在的--%>

    <%--爲""時,是分配了內存地址的,<logic:empty>依然爲空--%>

<%--<logic:notPresent>是存在的--%>

<%--屬性爲實例化的對象時,沒有實例化對象賦值empty還是空的[/code][/code]--%>
<%--present卻是存在的--%>

<%--這兩者的差別可以這麼理解--%>

<%--emtpy是判斷是否有實際的數據值--%>

<%--而present是看是否給分配過有效的內存地址--%>
    </body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章