搭建SSH框架,Struts2将Action对象托管给Spring容器来管理,配置文件目录概览
第一步安装插件,将Struts2-Spring-plugin.jar添加到项目的引用路径下。
第二步配置Struts.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.action.extension" value="do,shtml" /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- <constant name="struts.ui.theme" value="simple" /> --> <constant name="struts.devMode" value="false"/> <constant name="struts.multipart.maxSize" value="2024258888" /> <constant name="struts.multipart.saveDir" value="/tmp" /> <!-- 通过spring来管理Action --> <constant name="struts.objectFactory" value="spring" /> <!-- 引入其他文件--> <include file="conf/struts/struts_*.xml"/> </struts>
第三步配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 配置C标签 --> <jsp-config> <taglib> <taglib-uri>http://java.sun.com/jstl/core</taglib-uri> <taglib-location>/WEB-INF/c.tld</taglib-location> </taglib> </jsp-config> <!-- tomcat6 的配置 <taglib> <taglib-uri>http://java.sun.com/jstl/core</taglib-uri> <taglib-location>/WEB-INF/c.tld</taglib-location> </taglib> --> <!-- 配置struts2的过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>config</param-name> <param-value> struts-default.xml,struts-plugin.xml,conf/struts/struts.xml </param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 初始化Spring容器 ApplicationContext --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:conf/**/app_*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
第四步配置创建Action类添加到Struts2配置Action
//交给Spring管理 @SuppressWarnings("serial") @Controller @Scope("prototype") public class ContactsAction extends ActionSupport { private InputStream inputStream; public InputStream getInputStream() { return inputStream; } private String location; public String getLocation() { return location; } public String listContacts() throws IOException { // HttpServletRequest request = ServletActionContext.getRequest(); Map<String, String> map = new HashMap<String, String>(); map.put("flag", "success"); String retVal = GsonUtil.getInstance().convertToJson(map); inputStream = new ByteArrayInputStream(retVal.getBytes("UTF-8")); return SUCCESS; } }
配置Action
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="contacts" extends="struts-default" namespace="/contacts"> <action name="list" method="listContacts" class="org.lian.action.ContactsAction" > <result type="stream"> <param name="contentType">text/html</param> <param name="inputName">inputStream</param> </result> </action> </package> </struts>
页面访问路径
<a href="${ctx}/contacts/list.do">测试Action</a>