首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts1 入门范例

2012-09-20 
struts1 入门实例+++++++ 实现对学生资料的模糊查询 +++++++queryForm.jsp 中:body查询表单. brform

struts1 入门实例
+++++++ 实现对学生资料的模糊查询 +++++++

queryForm.jsp 中:

<body>
    查询表单. <br>
    <form action="/Prj2_1/query.do" method="post">
    请您输入学生姓名:<input name="sname">
    <input type="submit" value="模糊查询">
    </form>
</body>

queryResult.jsp中:

<body>
   显示结果. <br>
   <%
      ArrayList stus = (ArrayList)request.getAttribute("stus");
   %>
   <table bgcolor="#ff80ff">
   <tr>
   <td>学号</td>
   <td>姓名</td>
   <td>性别</td>
   <td>出生年月</td>
   <td>家庭住址</td>
   </tr>
   <%
   for(int i=0;i<stus.size();i++){
      Student stu = (Student)stus.get(i);
   %>
   <tr>
   <td><%=stu.getStuId() %></td>
   <td><%=stu.getStuName() %></td>
   <td><%=stu.getStuSex() %></td>
   <td><%=stu.getStuBir() %></td>
   <td><%=stu.getStuAdd() %></td>
   </tr>
   <% 
   }
   %>
   </table>
</body>

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配置文件中:

<struts-config>
<data-sources />
<!-- 在此注册ActionForm -->
<form-beans>
<!-- name:名称 type:类路径 -->
<form-bean name="queryForm" type="prj2_1.QueryForm"></form-bean>
</form-beans>

<global-exceptions />

<!-- 设置URL逻辑名称(全局,任意的Action都可以识别到)-->
<global-forwards>
<forward name="RESULT" path="/queryResult.jsp"></forward>
</global-forwards>

<!-- 注册Action -->
<action-mappings>
<!-- type:类路径; name:相应的ActionForm的名称; path:客户端提交到服务器端时指定的路径 -->
<action name="queryForm" path="/query" type="prj2_1.QueryAction">
<!-- 设置URL逻辑名称(局部,只有这个Action可以识别到)-->
   <forward name="RESULT" path="/queryResult.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="prj2_1.ApplicationResources" />
</struts-config>

热点排行