Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2321236
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: Java

2012-01-11 08:59:55

Struts层的功能开发 需要分4个步骤
1 开发Struts核心流程代码 包括AddressForm.java AddressAction.java及配置
2 开发JSP页面原型 包括数据列表页面address.jsp 增加页面address_add.jsp 修改页面address_edit.jsp并进行页面文字的国际化和样式修饰
3 增加表单效验功能 主要修改增加页面address_add.jsp   修改页面address_edit.jsp  表单类AddressForm.java
4 调用AddressDAO.java操作数据库  修改AddressAction.java调用AdressDAO.java中的函数 进行数据库的关联
 
 
Struts开发第一步
开发Struts核心流程代码
[1]新建并配置表单类AddressForm.java:配置元素 并开发AdressForm.java表单类
在增加或修改一个联系人对象时 需要一个表单来保存各个字段的数据 因此我们需要新建一个表单类AddressForm.java  该类中定义了与联系人表Address中的字段相对象的变量 并为每一个变量添加getter/setter函数
  1. package com.demo.struts.forms;


  2. import javax.servlet.http.HttpServletRequest;

  3. import org.apache.struts.action.ActionErrors;
  4. import org.apache.struts.action.ActionForm;
  5. import org.apache.struts.action.ActionMapping;

  6. import com.demo.struts.util.RegExpression;

  7. public class AddressForm extends ActionForm {


  8.     protected String id = null;

  9.     protected String username = null;

  10.     protected String name = null;

  11.     protected String *** = null;

  12.     protected String mobile = null;

  13.     protected String email = null;

  14.     protected String qq = null;

  15.     protected String company = null;

  16.     protected String address = null;

  17.     protected String postcode = null;

  18.     public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) {
  19.         ActionErrors errors = new ActionErrors();
  20.         
  21.         return errors;
  22.     }

  23.     public String getAddress() {
  24.         return address;
  25.     }

  26.     public void setAddress(String address) {
  27.         this.address = address;
  28.     }

  29.     public String getCompany() {
  30.         return company;
  31.     }

  32.     public void setCompany(String company) {
  33.         this.company = company;
  34.     }

  35.     public String getEmail() {
  36.         return email;
  37.     }

  38.     public void setEmail(String email) {
  39.         this.email = email;
  40.     }

  41.     public String getId() {
  42.         return id;
  43.     }

  44.     public void setId(String id) {
  45.         this.id = id;
  46.     }

  47.     public String getMobile() {
  48.         return mobile;
  49.     }

  50.     public void setMobile(String mobile) {
  51.         this.mobile = mobile;
  52.     }

  53.     public String getName() {
  54.         return name;
  55.     }

  56.     public void setName(String name) {
  57.         this.name = name;
  58.     }

  59.     public String getPostcode() {
  60.         return postcode;
  61.     }

  62.     public void setPostcode(String postcode) {
  63.         this.postcode = postcode;
  64.     }

  65.     public String getQq() {
  66.         return qq;
  67.     }

  68.     public void setQq(String qq) {
  69.         this.qq = qq;
  70.     }

  71.     public String get***() {
  72.         return ***;
  73.     }

  74.     public void set***(String ***) {
  75.         this.*** = ***;
  76.     }

  77.     public String getUsername() {
  78.         return username;
  79.     }

  80.     public void setUsername(String username) {
  81.         this.username = username;
  82.     }
  83. }
然后再struts-config.xml中添加一个 命名为addressForm 指向上面新建的表单类com.demo.struts.forms.AddressForm.java
       
    1. <form-bean name="addressForm"
    2.             type="com.demo.struts.forms.LoginForm">
    3. </form-bean>
     
    [2]在struts-config.xml中添加请求代理:配置元素 并委托给Spring进行管理
    对于通讯录模块 我们添加如下三个映射请求配置
    列表页请求 address.do
    新增请求  address_add.do
    修改请求  address_edit.do
    其中 type属性为org.springframework.web.struts.DelegatingActionProxy 表示将Struts的Action请求委托给Spring进行管理
    1. <action path="/address" parameter="method" name="pageForm" scope="request"
    2.             type="org.springframework.web.struts.DelegatingActionProxy">
    3.             <forward name="list" path="/address.jsp" />
    4.         </action>
    5.         <action path="/address_add" parameter="method" name="addressForm" scope="request"
    6.             type="org.springframework.web.struts.DelegatingActionProxy"
    7.             input="/address_add.jsp">
    8.             <forward name="list" path="/address.jsp" />
    9.             <forward name="add" path="/address_add.jsp" />
    10.         </action>
    11.         <action path="/address_edit" parameter="method" name="addressForm" scope="request"
    12.             type="org.springframework.web.struts.DelegatingActionProxy"
    13.             input="/address_edit.jsp">
    14.             <forward name="list" path="/address.jsp" />
    15.             <forward name="edit" path="/address_edit.jsp" />
    16.         </action>
    以上三个配置中 需要注意三点
       都有一个属性parameter 值为method 表示在Action类中 将使用method指向的参数作为执行函数的入口
       都包含了子元素 用以指定可能的返回页面地址 这些地址将会在AddressAction.java类被引用
       address_add.do address_edit.do的那么属性为上卖弄配置的表单组件addressForm 表示这两个请求需要使用该表单组件保存页面数据
     
    [3]在applicationContext.xml中添加请求代理 配置与相匹配的元素 接收Struts的委托
    上一节中配置的3个元素使用了org.springframework.web.struts.DelegatingActionProxy来委托给Spring进行管理 因此我们需要在applicationContext.xml中添加请求的代理配置组件 与的path属性想对应 需要添加如下3个请求代理
    列表页请求 address.do
    新增请求  address_add.do
    修改请求  address_edit.do
    其中的class书香均指向com.demo.struts.actions.AddressAction 表示由该类来处理请求 并为每一个注入addressDAO对象 该对象在Hibernate层开发中已经穿件并进行了配置
    1. </bean>
    2.     <bean name="/address"
    3.         class="com.demo.struts.actions.AddressAction">
    4.         <property name="addressDAO">
    5.             <ref local="addressDAO" />
    6.         </property>
    7.     </bean>
    8.     <bean name="/address_add"
    9.         class="com.demo.struts.actions.AddressAction">
    10.         <property name="addressDAO">
    11.             <ref local="addressDAO" />
    12.         </property>
    13.     </bean>
    14.     <bean name="/address_edit"
    15.         class="com.demo.struts.actions.AddressAction">
    16.         <property name="addressDAO">
    17.             <ref local="addressDAO" />
    18.         </property>
    19.     </bean>
     
    [4]在applicationContext.xml中添加日志拦截 为请求地址添加日志拦截器
    在Spring的applicationContext.xml中我们曾配置了日志拦截器 器拦截的目标是通过请求地址进行匹配拦截 现在新增加了3个关于通讯录模块的请求地址 因此我们需要对这3个地址的请求进行拦截 我们只需要在loggingAutoProxy组件中添加三行地址即可
    1. <!-- 配置拦截器 -->
    2.     <bean name="logger" class="com.demo.spring.aop.LoggingInterceptor" />

    3.     <!-- 配置拦截器代理 -->
    4.     <bean name="loggingAutoProxy"
    5.         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    6.         <property name="beanNames">
    7.             <list>
    8.                 <value>/login</value>
    9.                 <value>/register</value>
    10.                 <value>/address</value>
    11.                 <value>/address_add</value>
    12.                 <value>/address_edit</value>
    13.             </list>
    14.         </property>
    15.         <property name="interceptorNames">
    16.             <list>
    17.                 <value>logger</value>
    18.             </list>
    19.         </property>
    20.     </bean>
     
     
    [5]开发AdressAction.java原型功能:设计AdressAction.java的请求处理函数 这里的函数现在还只是空架子 只能够进行页面的流转 没有实际的功能 目的是为了在下一节开发完JSP页面后能够进行页面的流转
    现在已经在struts-config.xml中配置3个请求地址 并子啊applicationContext.xml中配置了请求代理 这里的请求会通过class属性找到Action类com.demo.struts.actions.AddressAction 该类才是真正处理用户请求的地方
    新建类com.demo.struts.actions.AddressAction.java 该类继承自Struts的鸡肋DispatcherAction 并为该类添加两个变量   AddressDAO类型变量addressDAO   日志记录器变量log
    该类继承自DispatcherAction 而且在struts-config.xml的配置中都指定了属性parameter=“method” 因此我们将在AddressAction.java 类中使用method指定的参数名作为函数的入口  不在限定于execute() 函数了 当然  method的参数值需要在后面的JSP页面中指定 我们先预设如下几种页面的请求函数  list  add  insert edit update delete
    我们先编写这6个函数的代码 实现简单的功能 只是查找中子元素定义的转发别名 返回ActionForword的对象即可  这只是最基础的流转代码 我们称之为原型
     
    1. package com.demo.struts.actions;

    2. import javax.servlet.http.HttpServletRequest;
    3. import javax.servlet.http.HttpServletResponse;

    4. import org.apache.log4j.Logger;
    5. import org.apache.struts.action.ActionForm;
    6. import org.apache.struts.action.ActionForward;
    7. import org.apache.struts.action.ActionMapping;
    8. import org.apache.struts.actions.DispatchAction;

    9. import com.demo.hibernate.dao.AddressDAO;
    10. import com.demo.struts.util.Constants;

    11. public class AddressAction extends DispatchAction {

    12.     protected AddressDAO addressDAO;

    13.     Logger log = Logger.getLogger(this.getClass());

    14.     public AddressDAO getAddressDAO() {
    15.         return addressDAO;
    16.     }

    17.     public void setAddressDAO(AddressDAO addressDAO) {
    18.         this.addressDAO = addressDAO;
    19.     }
    20.     
    21.     public ActionForward list(ActionMapping mapping, ActionForm form,
    22.             HttpServletRequest request, HttpServletResponse response)
    23.             throws Exception {    
    24.         ActionForward forward = mapping.findForward(Constants.LIST_KEY);
    25.         return (forward);
    26.     }

    27.     public ActionForward add(ActionMapping mapping, ActionForm form,
    28.             HttpServletRequest request, HttpServletResponse response)
    29.             throws Exception {        
    30.         ActionForward forward = mapping.findForward(Constants.ADD_KEY);
    31.         return (forward);
    32.     }

    33.     public ActionForward insert(ActionMapping mapping, ActionForm form,
    34.             HttpServletRequest request, HttpServletResponse response)
    35.             throws Exception {
    36.             ActionForward forword = mapping.findForward(Constants.ADD_KEY);
    37.             return (forword);
    38.     }

    39.     public ActionForward edit(ActionMapping mapping, ActionForm form,
    40.             HttpServletRequest request, HttpServletResponse response)
    41.             throws Exception {
    42.             return mapping.findForward(Constants.EDIT_KEY);
    43.     }

    44.     public ActionForward update(ActionMapping mapping, ActionForm form,
    45.             HttpServletRequest request, HttpServletResponse response)
    46.             throws Exception {
    47.         ActionForward forward = mapping.findForward(Constants.LIST_KEY);
    48.         return (forward);
    49.     }

    50.     public ActionForward delete(ActionMapping mapping, ActionForm form,
    51.             HttpServletRequest request, HttpServletResponse response)
    52.             throws Exception {
    53.         return mapping.findForward(Constants.LIST_KEY);
    54.     }
    55. }
    在常量类Constants.java中添加Key值
    1. package com.demo.struts.util;

    2. public class Constants {
    3.     public final static String USERNAME_KEY = "username";

    4.     public final static String SUCCESS_KEY = "success";
    5.     public final static String FAILURE_KEY = "failure";
    6.     public final static String LOGOUT_KEY = "logout";
    7.     public final static String LIST_KEY = "list";
    8.     public final static String ADD_KEY = "add";
    9.     public final static String EDIT_KEY = "edit";

    10. }
    阅读(2454) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~