通过前面几个例子!
我们对Struts解决Web问题有了简单的了解!
现在我们对Struts的架构做一个简单的解析!
- Struts架构资源列表(基于MyEclipse)
- Struts架构对于Web.xml的配置改动
我们都知道 servlet 的运行要通过配置 web.xml 来找到url 入口,而 Struts 同样是通过配置此文件来控制Struts架构入口
<!-- ... --> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <!-- 基于 Struts 架构的请求入口 表示所有以 .do 结尾的 servlet 请求将通过 Struts 架构解决 --> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- ... -->
以上图片为 MyEclipse 生成 ACtionForm的过程!现在我们来分析下其内容
用户自定义的实体Bean
package net.royakon.entity; public class User { private String userName; private String userPass; public User() { } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPass() { return userPass; } public void setUserPass(String userPass) { this.userPass = userPass; } }
用户提交数据的表单
<%@ page language="java" pageEncoding="gb2312"%> <form action="regAction.do" method="post"> <table> <tr> <td>num2</td> <td><input name="num2" type="text" /></td> </tr> <tr> <td>username</td> <!-- 当用对象保存用户提交数据时,控件名称需要以 对象.属性 形式进行命名 --> <td><input type="text" name="user.userName" /></td> </tr> <tr> <td>userpass</td> <!-- 当用对象保存用户提交数据时,控件名称需要以 对象.属性 形式进行命名 --> <td><input type="text" name="user.userPass" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="Reg"/></td> </tr> </table> </form>
MyEclipse Struts生成的ActionForm
/** * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package cn.net.royakon.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: 12-22-2008 * * XDoclet definition: * @struts.form name="addForm" */ public class AddForm extends ActionForm { /** * 用户表单传递至Struts的数据需要保存到相应的ActionForm * 其中每个属性表示一个用户提交的数据 * 对象同样能够作为属性用于保存用户表单提交的数据->需要对对象进行实例化! */ /** num2 property */ private double num2; /** num1 用户自定义的实体Bean */ private User user = new User(); /** * Generated Methods */ /** * 用于表单数据验证的方法 * @param mapping * @param request * @return ActionErrors */ public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub return null; } /** * 用于表单重置的方法 * @param mapping * @param request */ public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub } /** * Returns the num2. * @return double */ public double getNum2() { return num2; } /** * Set the num2. * @param num2 The num2 to set */ public void setNum2(double num2) { this.num2 = num2; } /** * Returns the user. * @return User */ public User getUser() { return user; } /** * Set the user. * @param user The user to set */ public void setUser(User user) { this.user = user; } }
同时 struts-config.xml 生成了如下数据为struts指明ActionForm别名
<form-beans > <form-bean name="addForm" type="cn.net.royakon.form.AddForm" /> </form-beans>
以上图片为 MyEclipse 生成 ACtion的过程!现在我们来分析下其内容:
MyEclipse Struts生成的Action
/** * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package cn.net.royakon.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 cn.net.royakon.form.AddForm; import cn.net.royakon.model.CalcModel; /** * MyEclipse Struts * Creation date: 12-22-2008 * * XDoclet definition: * @struts.action path="/addAction" name="addForm" scope="request" validate="true" * @struts.action-forward name="result" path="/relult.jsp" */ public class AddActionAction 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) { AddForm addForm = (AddForm) form; //取得ActionForm数据 User user = addForm.getUser(); double num2 = addForm.getNum2(); //...实现用户功能 request.setAttribute("result", result); //结束用户功能后执行跳转->按照别名跳转 return mapping.findForward("result"); } }
同时 struts-config.xml 生成了如下数据为struts指明Action
attribute – 指明Action数据关联的ActionForm
path – 用户表单提交的目的地:addAction.do
forward – 用户功能结束后跳转别名
<!-- ... --> <action attribute="addForm" name="addForm" path="/addAction" scope="request" type="cn.net.royakon.action.AddActionAction"> <forward name="result" path="/relult.jsp" /> </action> <!-- ... -->
Categories: 网页编程