小案例: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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值