参考http://www.iteye.com/problems/79605
spring的配置:
1.classpath*:spring.xml
注意:红字部分是必须包含的,否则不能编译通过,标签内容是关于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