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