zoukankan      html  css  js  c++  java
  • 小案例:struts1.3利用nested标签使用POJO

    其中的关键就是这个POJO是你自己去new一个,struts是不会帮你创建的!参考http://luohua.iteye.com/blog/39976

    表单页

    <%@ page language="java" contentType="text/html; charset=GBK"
        pageEncoding="GBK"%>
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
    <%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title>Insert title here</title>
    </head>
    <body>
        <span style="color:red"><html:errors property="emptycontent"/></span>
        <html:form action="/jwid/struts1x/14.5/usebean.do" method="post">
            INFO:<html:text property="info"/><br>
                <nested:nest property="person">
                    姓名:<nested:text property="name"/><br>
                    年龄:<nested:text property="age"/><br>
                </nested:nest>
            <html:submit value="提交"/><html:reset value="重置"/>
        </html:form>
    </body>
    </html>

    ActionForm

    public class UseBeanForm extends ActionForm {
        
        private Person person = new Person(); // You must initialize this bean by yourself!!!
        private String info;
        public String getInfo() {
            return info;
        }
        public Person getPerson() {
            return person;
        }
        public void setInfo(String info) {
            this.info = info;
        }
        public void setPerson(Person person) {
            this.person = person;
        }
    
        @Override
        public ActionErrors validate(ActionMapping mapping,
                HttpServletRequest request) {
            ActionErrors errors = new ActionErrors();
            if (info == null || info.trim().length() == 0) {
                errors.add("emptycontent", new ActionMessage("jwid.c14.section14dot5.emptyconent"));
            }
            if (person == null || person.getName().trim().length() == 0 || person.getAge() <= 0) {
                errors.add("emptycontent", new ActionMessage("jwid.c14.section14dot5.emptyconent"));
            }
            return errors;
        }
    
    }

    Action

    public class UseBeanAction extends Action {
    
        @Override
        public ActionForward execute(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            UseBeanForm useBeanForm = (UseBeanForm)form;
            String info = useBeanForm.getInfo();
            if (info.length() > 5) {
                return mapping.findForward("infoMore");
            } else {
                return mapping.findForward("infoLess");
            }
        }
    
    }

    结果页1 info_less.jsp

    <%@ page language="java" contentType="text/html; charset=GBK"
        pageEncoding="GBK"%>
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title>Insert title here</title>
    </head>
    <body>
        info.length &lt;= 5<br>
        ${requestScope.info }<br>
        ${requestScope.person.name }<br>
        ${requestScope.person.age }
    </body>
    </html>

    结果页2 info_more.jsp

    <%@ page language="java" contentType="text/html; charset=GBK"
        pageEncoding="GBK"%>
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
    <title>Insert title here</title>
    </head>
    <body>
        info.length &gt; 5<br>
        ${requestScope.info }<br>
        ${requestScope.person.name }<br>
        ${requestScope.person.age }
    </body>
    </html>

    struts-config.xml

    <form-bean name="useBeanForm"
                type="jwid.c14.section14dot5.UseBeanForm" />
    
    <action attribute="useBeanForm" name="useBeanForm" scope="request"
                path="/jwid/struts1x/14.5/usebean" input="/jwid/struts1x/14.5/use_bean.jsp"
                type="jwid.c14.section14dot5.UseBeanAction">
                <forward name="infoMore" path="/jwid/struts1x/14.5/info_more.jsp"/>
                <forward name="infoLess" path="/jwid/struts1x/14.5/info_less.jsp"/>
            </action>
  • 相关阅读:
    HDU 3848 CC On The Tree 树形DP
    编程求取直线一般式表达式,两直线交点
    向外国学者所要论文源代码--英语模版
    找出该树中第二小的值--思路及算法实现
    不使用额外空间交换2个数据的源代码
    华为2018软件岗笔试题解题思路和源代码分享
    华为笔试题--LISP括号匹配 解析及源码实现
    Linux 快捷键汇总(偏基础)
    快速排序算法思路分析和C++源代码(递归和非递归)
    Python读取SQLite文件数据
  • 原文地址:https://www.cnblogs.com/qrlozte/p/3554320.html
Copyright © 2011-2022 走看看