ITeye博客

struts1 入门实例

lin870521 · 2010-11-04 16:46 · 企业架构 · Apache · Struts · XML · JSP · Markdown

+++++++ 实现对学生资料的模糊查询 +++++++ queryForm.jsp 中: 查询表单.

请您输入学生姓名:

queryResult.jsp中: 显示结果.
<% ArrayList stus = (ArrayList)request.getAttribute("stus"); %> <% for(int i=0;i <% } %>

学号 姓名 性别 出生年月 家庭住址 <%=stu.getStuId() %> <%=stu.getStuName() %> <%=stu.getStuSex() %> <%=stu.getStuBir() %> <%=stu.getStuAdd() %>

QueryForm.java中: //ActionForm:容纳表单提交的值 //1:必须继承 org.apache.struts.action.ActionForm //2:必须编写和表单元素同名的属性 //3:必须在Struts配置文件中进行注册 public class QueryForm extends ActionForm{ public QueryForm(){ System.out.println("QueryForm构造函数"); } private String sname; public String getSname() { return sname; } public void setSname(String sname) { System.out.println("QueryForm setSname"); this.sname = sname; } } QueryAction中: //Action:负责接受ActionForm的数据,处理 //1:必须继承org.apache.struts.action.Action //2:重写execute方法来处理业务逻辑 //3:将这个类在配置文件中注册 public class QueryAction extends Action{ public QueryAction(){ System.out.println("QueryAction构造函数"); } //ActionForward:封装了跳转目标的路径 //mapping:访问配置文件 //form:传过来的ActionForm对象 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { QueryForm queryForm = (QueryForm)form; String sname = queryForm.getSname(); //String sname = request.getParameter("sname");在一般情况下可以不要Form(如:QueryForm此处可以省去),用这条代码获得表单元素值 , 但是如果用到Struts标签就必须要Form了 sname = new String(sname.getBytes("ISO-8859-1")); StudentDao studentDao = new StudentDao(); ArrayList stus = studentDao.queryStuByName(sname); request.setAttribute("stus", stus); //跳转 //ActionForward af = new ActionForward("/queryResult.jsp"); ActionForward af = mapping.findForward("RESULT"); return af; } } struts-config.xml配置文件中:

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。