小案例:struts1.3利用nested标签使用POJO
2014-02-18 15:20
405 查看
其中的关键就是这个POJO是你自己去new一个,struts是不会帮你创建的!参考http://luohua.iteye.com/blog/39976
表单页
ActionForm
Action
结果页1 info_less.jsp
结果页2 info_more.jsp
struts-config.xml
表单页
<%@ 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>
相关文章推荐
- Struts的logic标签使用注意事项(随时补充)
- STRUTS2 常用标签的使用
- 使用struts1.2 标签 判断字符串长度,截取字符串。
- android开发之TabHost分页标签使用案例(带滑动效果)
- 4-Struts标签的使用--logic(empty, notEmpty, present, notPresent)
- 【struts】——bean_write标签使用
- struts的logic标签详细使用方法及举例说明
- struts数据标签的使用
- 使用Struts提供的form相关的标签
- Struts 2中<s: radio> 标签使用
- Struts Nested标签库
- 使用Struts标签之iterator
- struts标签使用举例--logic篇
- 进入模板页不能使用struts标签问题
- JSP标签使用案例
- Struts 2 标签使用的总结
- struts标签使用举例--logic篇【转】
- Angular1.3.X中embed标签使用ng-src的bug
- struts标签库:配置和使用
- Struts1 bean标签的使用