`

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

 

分享到:
评论

相关推荐

    用struts 1.x写的一个图书管理系统的例子

    本文将深入探讨一个使用Struts 1.x框架编写的图书管理系统的实例,该系统支持HTML和JSP页面,同时也集成了MySQL数据库和SSH(Spring、Struts、Hibernate)中的Struts组件。这个项目对于初学者来说是一个很好的学习...

    Struts2教程

    让我们先来回顾一下建立基于 Struts1.x 的 Web 程序的基本步骤。 1. 安装 Struts 。由于 Struts 的入口点是 ActionServlet ,所以得在 web.xml 中配置一下这 个 Servlet 。 2. 编写 Action 类(一般从 org.apache....

    Struts2教程:获得HttpServletResponse对象

    在深入探讨如何在Struts2框架中获取`HttpServletResponse`对象之前,我们先来简要回顾一下Struts2框架的基本概念及其与传统Struts1.x的区别。 #### Struts2框架简介 Struts2是Apache软件基金会的一个开源项目,它...

    mvc3.rar_mvc3

    Struts 1.x是早期流行的MVC框架,而MVC3可能是对Struts的一种扩展或者是一个类似的框架,它帮助开发者更好地理解Struts的架构和工作原理。 描述中提到"MVC框架"是"Struts 1.x的前身",这可能意味着MVC3是Struts系列...

    javaEE项目开发实训设计说明书.doc

    1. **Struts2.x的使用** Struts2.x是Apache软件基金会的一个开源项目,它扩展了MVC设计模式,使得前端展示和业务逻辑得以分离。在实训设计中,学生需要掌握如何利用Struts2.x开发系统的前台页面和业务逻辑。Struts2...

    张孝祥09年struts高级实战进阶PPT

    需要注意的是,Struts 1.x虽然经典,但在现代开发中已被Struts 2或其他更现代的框架(如Spring MVC)所取代,学习时也应关注新技术的发展。不过,了解Struts 1的基础和原理对于理解其他框架的工作方式仍有帮助。

    struts入门教程(比较详细,经验心得)

    本教程提供的`struts1.x入门[修订版].doc`文档可能会涵盖以上知识点的详细内容,而`readme,3Q.txt`可能是阅读指南或作者的感谢信息。对于有超过一年工作经验的开发者,虽然这个入门教程可能过于基础,但回顾基础...

    spring2.5+struts2.0+hibernate3.0例子 下载

    Struts 2是一个基于MVC(Model-View-Controller)设计模式的Web应用程序框架,它是Struts 1.x的升级版,提供了更强大的功能和灵活性。Struts 2.0引入了拦截器机制,允许开发者创建可重用的行为组件,提高了代码的...

    Java实训教程 Java软件开发实战 Java开发框架介绍 struts2_8_文件上传与下载 共20页.pptx

    在深入了解Struts2文件上传与下载的具体实现之前,我们先来回顾一下Struts2的基础概念及其课程大纲概览。 - **Struts2**是一种基于Java的Web应用程序框架,它采用了MVC(Model-View-Controller)架构模式。 - 该...

    Java框架研发思考.docx

    Jdon框架的初衷是整合Struts1.x(表现层框架)、Spring(业务层框架,包含DI和AOP)以及PicoContainer(轻量级容器),以简化开发流程。然而,作者并不满足于仅仅集成现有框架,他希望实现业务层和持久层的紧密集成...

    MyEclipse_JSF中文版(程序员必看,精心整理,免分数).pdf

    介绍章节简单介绍了JSF的基本概念,同时考虑到有些读者可能有Struts背景,文档会提及JSF与Struts的相似点,但同时也表明即使没有Struts知识,也能顺利学习JSF。 系统需求章节列出了开发所需的软件,包括JDK 1.4+、...

    湖科大2021JavaWeb实验及课程设计-图书信息管理系统-.zip

    在IntelliJ IDEA 2021.1 x64环境下进行开发,意味着项目使用了这个强大的集成开发环境,它提供了丰富的Java和Web开发工具,如代码提示、调试器、版本控制集成等,极大地提高了开发效率。 总的来说,这个项目涵盖了...

Global site tag (gtag.js) - Google Analytics