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>
  • 相关阅读:
    总结几个 webpack 打包优化的方法,前端项目必备
    vue-cli 3.0 axios 跨域请求代理配置及生产环境 baseUrl 配置
    React之MobX使用
    Couldn't load this key (OpenSSH SSH-2 private key(old PEM format))的解决办法
    HTML基础篇(一,认识HTML)
    Angular学习之路-一、配置项目
    小程序开发日志-3、调用相机竖屏拍照,并将照片转横屏显示
    自定义handsome主题默认文章头图
    DruidDataSource无限重连(mybatis数据源)
    mysql获取表字段信息(字段名,字段长度,字段类型,精度,小数点位)
  • 原文地址:https://www.cnblogs.com/qrlozte/p/3554320.html
Copyright © 2011-2022 走看看