java-怎样在main方法中调用action

以下内容转自http://blog.csdn.net/zzq900503/article/details/40590281

Javaspring 框架 中的action操作(extends ActionSupport) 一般配置在struts中 通过 浏览器 通过链接形式访问 

 

例如:我的action和配置文件如下:

RapToDpsProjectRaw.java

 

[java] view plain copy
 
  1. package  com.action.rap;  
  2.   
  3. public class RapToDpsProjectRaw extends ActionSupport {  
  4.   
  5.   
  6.     /** 
  7.      *  
  8.      */  
  9.     private static final long serialVersionUID = 4759438181190004544L;  
  10.   
  11.   
  12.     private DpsProjectRapReposity dpsProjectRapReposity;  
  13.   
  14.   
  15.     public void setDpsProjectRapReposity(  
  16.             DpsProjectRapReposity dpsProjectRapReposity) {  
  17.         this.dpsProjectRapReposity = dpsProjectRapReposity;  
  18.     }  
  19.   
  20.   
  21.     public String execute() {  
  22.         System.out.println("已执行");     
  23.   
  24.         return SUCCESS;  
  25.     }  



rap.xml    spring文件

[java] view plain copy
 
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <!-- 指定Spring配置文件的Schema信息 -->  
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  7.     <!-- 查询LINKURL -->      
  8.       
  9.   
  10.   
  11.     <bean id="rapToDpsProjectRaw" class="com.action.rap.RapToDpsProjectRaw">  
  12.         <property name="dpsProjectRapReposity" ref="dpsProjectRapReposity"></property>  
  13.     </bean>  
  14. </beans>  



rap.xml              struts文件

[java] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.1.7.dtd">  
  5. <struts>  
  6.     <package name="rapToDpsProjectRaw" extends="imoen-default" namespace="/rap">    
  7.         <action name="rapToDpsProjectRaw" class="rapToDpsProjectRaw">  
  8.             <result type="json"></result>  
  9.         </action>                   
  10.     </package>  
  11. </struts>  



 

我要执行这个action  就会在浏览器中输入  http://localhost:8080/项目名/rap/rapToDpsProjectRaw

 

 

如果要在 main方式中 运行  主要是获取到 rapToDpsProjectRaw这个Bean  代码如下:

 

[java] view plain copy
 
    1. public static void main(final String[] args) {  
    2.         LOG.info("starting to copy record from rap system to mongo db...");  
    3.         final String[] paths = new String[] {  
    4.                 "resource/spring/action/rap/rap.xml",  
    5.                 "resource/struts/rap.xml", "resource/spring/common-beans.xml",  
    6.                 "resource/spring/reposity/common-reposity.xml" };  
    7.         @SuppressWarnings("resource")  
    8.         final ApplicationContext context = new FileSystemXmlApplicationContext(  
    9.                 paths); // ClassPathXmlApplicationContext  
    10.         final RapToDpsProjectRaw processor = (RapToDpsProjectRaw) context  
    11.                 .getBean("rapToDpsProjectRaw");  
    12.         final String result = processor.execute();  
    13.         if (ActionSupport.SUCCESS.equals(result)) {  
    14.             LOG.info("Good job, copy operation succedded!");  
    15.         } else {  
    16.             LOG.warn("Oops! There's something wrong under the hook!");  
    17.         }  
    18.         LOG.info("finished copying record from rap system to mongo db.");  
    19.     }  

 

 

=============================华丽的分割线====================================

以下内容为自己的感悟:

我认为还是从web的原理入手,将context给加载起来了,可以使用该种方法进行web工程中的测试。

posted on 2017-06-18 08:55  傻瓜乐园  阅读(170)  评论(0)    收藏  举报

导航