




已阅读5页,还剩23页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Struts1配置实例1 struts1之初来乍到Step1 使用myeclipse向导引入strut1.2web.xml文件里面自动生成代码: action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml debug 3 detail 3 0 action *.do index.jsp Step2 配置文件struts-config.xml !- - !- -对应的逻辑图:Step3 包com.struts.form的HelloForm.java/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */package com.struts.form;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionMapping;/* * MyEclipse Struts * Creation date: 03-15-2014 * * XDoclet definition: * struts.form name=helloForm */public class HelloForm extends ActionForm /* * Generated fields */* name property */private String name;/* * Generated Methods */* * Method validate * param mapping * param request * return ActionErrors */public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) / TODO Auto-generated method stubreturn null;/* * Method reset * param mapping * param request */public void reset(ActionMapping mapping, HttpServletRequest request) / TODO Auto-generated method stub/* * Returns the name. * return String */public String getName() return name;/* * Set the name. * param name The name to set */public void setName(String name) = name;包com.struts.action的HelloAction.java/* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */package com.struts.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import com.struts.form.HelloForm;/* * MyEclipse Struts * Creation date: 03-15-2014 * * XDoclet definition: * struts.action path=/hello name=helloForm input=/form/hello.jsp scope=request validate=true */public class HelloAction extends Action /* * Generated Methods */* * Method execute * param mapping * param form * param request * param response * return ActionForward */public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) HelloForm helloForm = (HelloForm) form;/ TODO Auto-generated method stubif(helloForm.getName()=null|helloForm.getName().trim().length()=0)/返回输入页面 就是/form/hello.jsp,配置在struts-config.xml中return mapping.getInputForward();return mapping.findForward(success);Step4 包/itatis1.1/WebRoot/form下的hello.jsp JSP for HelloForm formname : 包/itatis1.1/WebRoot/form下的helloFail.jsp Fail Jsp error 包/itatis1.1/WebRoot/form下的helloSuccess.jsp My JSP for helloForm Hello,$helloF .Welcome to Struts world. Step5 在浏览器地址栏中输入http:/localhost:8080/itatis1.1/hello.do单击Submit按钮上面例子的升华版本提交用户表单信息包/itatis1.1/WebRoot/form下的hello.jspJSP for HelloForm form姓名 : 年龄 : 是否通过struts : 爱好: 音乐体育影视 日期 : 时间 : 包/itatis1.1/WebRoot/form下的helloSuccess.jspMy JSP for helloForm您好,$helloF . 您的年龄是:$helloForm.age .你以前用过struts你以前没有用过struts 您的爱好是: $hobby 日期:$helloForm.date 时间:$helloForm.time 包com.struts.form下的HelloForm.javapackage com.struts.form;import java.sql.Date;import java.sql.Time;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionErrors;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionMapping;/* * struts.form name=helloForm */public class HelloForm extends ActionForm /* * Generated fields */private String name;private String hobby;private int age;private boolean exprience;private Date date;private Time time;/* * Generated Methods */* * Method validate * param mapping * param request * return ActionErrors */public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) return null;/* * Method reset * param mapping * param request */public void reset(ActionMapping mapping, HttpServletRequest request) / TODO Auto-generated method stub/* * Returns the name. * return String */public String getName() return name;/* * Set the name. * param name The name to set */public void setName(String name) = name;public String getHobby() return hobby;public void setHobby(String hobby) this.hobby = hobby;public int getAge() return age;public void setAge(int age) this.age = age;public boolean isExprience() return exprience;public void setExprience(boolean exprience) this.exprience = exprience;public Date getDate() return date;public void setDate(Date date) this.date = date;public Time getTime() return time;public void setTime(Time time) this.time = time;包com.struts.action下的HelloAction.javapackage com.struts.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import mons.beanutils.ConvertUtils;import mons.beanutils.converters.SqlDateConverter;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import com.struts.form.HelloForm;public class HelloAction extends Action /* * Method execute * param mapping * param form * param request * param response * return ActionForward */public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) ConvertUtils.register(new SqlDateConverter(null), java.sql.Date.class);HelloForm helloForm = (HelloForm) form;/ TODO Auto-generated method stubif(helloForm.getName()=null|helloForm.getName().trim().length()=0)/返回输入页面 就是/form/hello.jsp,配置在struts-config.xml中return mapping.getInputForward();return mapping.findForward(success);添加包com.ibatisdemo.filter下的类CharacterEncodiingFilter.javapackage com.ibatisdemo.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;public class CharacterEncodiingFilter implements Filter private String characterEncoding;private boolean enabled;public void destroy() characterEncoding=null;public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException if(enabled|characterEncoding!=null)request.setCharacterEncoding(characterEncoding);response.setCharacterEncoding(characterEncoding);chain.doFilter(request, response);public void init(FilterConfig config) throws ServletException characterEncoding=config.getInitParameter(characterEncoding);enabled=true.equalsIgnoreCase(config.getInitParameter(enabled).trim();在web.xml文件中添加此过滤器 characterEncodingFilter com.ibatisdemo.filter.CharacterEncodiingFilter characterEncoding UTF-8 enabled true characterEncodingFilter /* action org.apache.struts.action.ActionServlet config /WEB-INF/struts-config.xml debug 3 detail 3 0 action *.do index.jsp 效果:总结本节主要涉及提交用户信息时表单对象的信息的自动转化(由struts1负责转化,开发人员不需要去强制转化),struts标签和jstl标签的,另外,本例子还涉及一个字符编码过滤器的编写及配置,下面是struts的常用标签库:1. 2. 3. 4. 5. 6. 7. 表单验证在HelloForm中:/* * Method validate * param mapping * param request * return ActionErrors */public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) ActionErrors errors=new ActionErrors();/保存所有错误信息if(name=null|name.trim().length()=0)errors.add(name,new ActionMessage();if(hobby=null|hobby.length1)errors.add(hobby,new ActionMessage(hello.error.hobby);if(age5)errors.add(age,new ActionMessage(hello.error.age,5);return errors;/* * Method reset * param mapping * param request */public void reset(ActionMapping mapping, HttpServletRequest request) age=5;/初始化数据在包com.struts的文件ApplicationRperties添加=u540Du5B57u4E0Du80FDu4E3Au7A7Ahello.error.hobby=u7231u597Du5FC5u987Bu586Bu5199hello.error.age=u5E74u9F84u4E0Du80FDu5C0Fu4E8E5u5C81效果:提交对象属性提交数据量很大时,我有必要把某些对象的属性在前台用 对象.属性的形式进行提交,这样后台不会因为属性多而烦恼。在com.ibatisdemo.entity里面新建一个类Person.java package com.ibatisdemo.entity;public class Person /账号private String account;/姓名private String name;public Person() super();public String getAccount() return account;public void setAccount(String account) this.account = account;public String getName() return name;public void setName(String name) = name;在包com.struts.form新建对应的Form表单对象UseBeanForm.javapackage com.struts.form;import org.apache.struts.action.ActionForm;import com.ibatisdemo.entity.Person;public class UseBeanForm extends ActionFormprivate String action;private Person person=new Person();public String getAction() return action;public void setAction(String action) this.action = action;public Person getPerson() return person;public void setPerson(Person person) this.person = person;在包com.struts.action下新建UseBeanAction.javapackage com.struts.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import com.struts.form.HelloForm;import mons.beanutils.ConvertUtils;import com.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 产品组合管理
- 幼小衔接培训老师
- 医院冬季消防法律培训
- 销售月度工作总结及计划
- 儿童哮喘护理
- 表单填写说明培训
- 有效沟通机制培训
- 职业教育管理学理论与实践
- 肢体无力护理查房
- 子宫颈癌护理诊断
- 企业市场营销策略顾问服务合同范本
- 贵州省贵阳市部分学校2024-2025学年高二下册期末联考数学试卷(附答案)
- 2024-2025学年人教版 2024小学数学一年级下册教学工作总结(共3套)
- 学生高层宿舍管理制度
- 薪资发放协议
- T/CAR 7-2021绿色高效自携式商用冷藏陈列柜技术要求和评价方法
- 合作账号合伙协议书
- 2025-2030年国内冷藏集装箱行业市场现状供需分析及投资评估规划分析研究报告
- 警务技能抓捕课件
- 广东省广州市南沙区2025届七下生物期末教学质量检测试题含解析
- DB13T 2700-2018 水工柔性生态防护结构设计规范
评论
0/150
提交评论