struts基础回顾

1.struts2在开发中所必须用到的jar包导入到项目的lib目录下



2.在web.xml中配置一个过滤器,代码格式如下

[html]  view plain copy print ?
  1. <filter>  
  2.     <filter-name>struts2</filter-name>  
  3.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  4. </filter>  
  5.   
  6. <filter-mapping>  
  7.     <filter-name>struts2</filter-name>  
  8.     <url-pattern>/*</url-pattern>  
  9. </filter-mapping>  

当然,大家应该知道struts2的核心技术是基于拦截器实现的

3.写一个简单的Action

CustomerAction.java

[java]  view plain copy print ?
  1. package com.sdu.crm.action;  
  2.   
  3. import org.directwebremoting.util.SystemOutLoggingOutput;  
  4.   
  5. import com.opensymphony.xwork2.Action;  
  6. import com.opensymphony.xwork2.ActionSupport;  
  7. import com.opensymphony.xwork2.ModelDriven;  
  8. import com.sdu.crm.dao.impl.CustomerDaoImpl;  
  9. import com.sdu.crm.pojo.Customer;  
  10. import com.sun.net.httpserver.Authenticator.Success;  
  11.   
  12. public class CustomerAction extends ActionSupport  {  
  13.   
  14.     private static final long serialVersionUID = 1L;  
  15.   
  16.     private Customer customer;  
  17.   
  18.     public Customer getCustomer() {  
  19.         return customer;  
  20.     }  
  21.     public void setCustomer(Customer customer) {  
  22.         this.customer = customer;  
  23.     }  
  24.   
  25.     /** 
  26.      *  
  27.      * @author fighter24h 
  28.      * 自定义的action方法 
  29.      * 其形式同execute方法 
  30.      * 返回String,抛出Exception 
  31.      * 方法名字对应struts配置文件中的method属性 
  32.      */  
  33.     public String addCustomerInfo() throws Exception {  
  34.         return "addCustomerInfoOk";  
  35.     }  
  36.   
  37. }  

4.配置acton对应的strtus的xml文件

示例:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5. <struts>  
  6.     <!-- package name:用来实现继承的  namespace:用来解决重名的,访问的时候充当路径-->  
  7.     <package name="action-customer-lzm" extends="struts-default"  
  8.         namespace="/customer">  
  9.         <!--  
  10.             name:action的名字,访问的使用需要用到 class:具体的action是哪个类,写全路径  
  11.             method:我们自己定义的方法,而不是执行execute方法  
  12.         -->  
  13.         <action name="addCustomerInfo" class="com.sdu.crm.action.CustomerAction"  
  14.             method="addCustomerInfo">  
  15.             <!--name:来自method的返回值,可以自定义!  -->  
  16.             <result name="addCustomerInfoOk">/index.jsp</result>  
  17.         </action>  
  18.     </package>  
  19. </struts>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值