关于spring与struts2使用Annotion注解方式的集成

参考http://www.iteye.com/problems/79605

 spring的配置:

1.classpath*:spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context    
   
http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 使用 annotation -->
 <context:annotation-config />
 
 <!-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 -->
 <context:component-scan base-package="com.at" />

</beans>

注意:红字部分是必须包含的,否则不能编译通过,标签内容是关于annotion方式注册。

2.web.xml

 

<!-- 加载Spring容器配置 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath*:spring.xml
  </param-value>
 </context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

 

struts的配置

1.classpath*:struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 

<struts>

<!--  struts.devMode : 是否设置为开发模式 true:是开发模式,否则不是
  注:在开发模式下,修改Struts的配置文件后不需要重新启动Tomcat服务器即生效。
    否则修改Struts配置文件后需要重新启动Tomcat服务器才生效。

-->
<constant name="struts.devMode" value="false" />
<!-- 关闭动态方法调用,(关闭action名 + 感叹号 + 方法名进行方法调用,如login!checkLogin.action,调用Action名为login类中的checkLogin方法) -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
     <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.action.extension" value="do" />
    <constant name="struts.i18n.encoding" value="utf-8"/> 
    
<!-- 加载模块 -->
   <include file="struts_fee.xml"/>
</struts>

2.web.xml

    <filter>
        <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

3.strust2模块配置文件struts_fee.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 

<struts>
 <package name="fee" extends="struts-default" namespace="/fee">
 
  <action name="searchFeeInfo" class="feeInfoAction" method="searchFeeInfo">
    <!-- <result name="success">/feeInfoIndex.jsp</result>-->
  </action>
  
 </package>
</struts>

action类FeeInfoAction.java

package com.at.fee.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.at.fee.model.FeeInfo;
import com.at.fee.service.FeeInfoService;
import com.at.tsframe.base.BaseStruts2Action;

@Scope("prototype")
@Controller("feeInfoAction")

public class FeeInfoAction extends BaseStruts2Action {
 private static final long serialVersionUID = -1793059480215383708L;
 @Resource(name= "feeInfoService")
 private FeeInfoService feeInfoService;

 public String searchFeeInfo(){
  
  List<FeeInfo> listFeeInfo=feeInfoService.searchFeeInfos();
  JSONObject result=new JSONObject();
        JSONArray rows = JSONArray.fromObject(listFeeInfo);
        result.put("rows", rows);
        result.put("total", listFeeInfo.size());
  String jsonResult = result.toString();
  HttpServletResponse response = ServletActionContext.getResponse();
  response.getWriter().write(jsonResult);
  
  return null;
 }

}

 service类feeInfoService.java

@Service
public class FeeInfoService implements IFeeInfoService {
 private FeeInfoDao feeInfoDao=new FeeInfoDao();

。。。

}

 jsp页面调用

http://xxx/fee/searchFeeInfo.do

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云焰

你的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值