Struts1.x使用回顾

1. download the jar file and put them under /WEB-INF/lib; downland  taglib description *.tld file and put them under /WEB-INF (you can change the path, eg. /WEB-INF/tld/ which should be consitent with the configuration in web.xml)

2. modify web.xml to include

 

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>3</param-value>

</init-param>

<init-param>

<param-name>detail</param-name>

<param-value>3</param-value>

</init-param>

<load-on-startup>0</load-on-startup>

</servlet>

 

 

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

 

 

<jsp-config>

<taglib>

<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-html.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>

</taglib>

</jsp-config>

 

 

3. add struts-config.xml under  /WEB-INF (which is configured in web.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-mappings>

<action

                path="/Welcome"

                forward="/jsp/welcome.jsp"/>

<action path="/beanWrite" type="com.tag.BeanWriteAction">

<forward name="success" path="/jsp/tag_beanwrite.jsp"></forward>

</action>

 

<action path="/logic" type="com.tag.LogicAction">

<forward name="success" path="/jsp/tag_logic.jsp"></forward>

</action>

 

<action path="/iterator" type="com.tag.IteratorAction">

<forward name="success" path="/jsp/tag_iterator.jsp"></forward>

</action>

</action-mappings>

 

</struts-config>

 

4. add a jsp file name welcome.jsp and put it under /WebContent/jsp, then you can visit the url

http://localhost:9080/Demo/Welcome.do in explorer, you will see the content of welcome.jsp.

please note url in case-sensitive, so /Welcome.do and /welcome.do are different.

 

5. bean write

 

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;
}
 

 

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");
                
        }
}

 

<%--
    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="/WEB-INF/struts-bean.tld" %>

<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>
 

http://localhost:9080/Demo/beanWrite.do

 

6. logic

 

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");
                
        }
}

 <%--

    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>

 http://localhost:9080/Demo/logic.do

 

7. iterator

 

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");
    }
}

 <%--

    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>

 http://localhost:9080/Demo/iterator.do

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值