平时我们习惯用了spring整合struts2,今天我们就来见识一下guice整合struts2吧。
看web.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <filter> <filter-name>TransactionFilter</filter-name> <filter-class>com.ming.core.web.filter.TransactionFilter</filter-class> </filter> <filter-mapping> <filter-name>TransactionFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>guiceFilter</filter-name> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> </filter> <filter-mapping> <filter-name>guiceFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>com.ming.core.web.listener.GoogleGuiceServletContextListener</listener-class> </listener> <!-- struts2的过滤器 --> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
然后是struts.xml 的配置:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 引用guice代理 --> <constant name="struts.objectFactory" value="guice" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <include file="com/ming/user/action/userStruts.xml"></include> </struts>
其次是userStruts.xml的配置:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="com.ming.user.action" namespace="/user" extends="struts-default"> <action name="*_*" class="com.ming.user.action.{1}Action" method="{2}"> <result name="data" type="stream"> <param name="contentType">text/html</param> <param name="inputName">inputStream</param> </result> </action> </package> </struts>
然后我们看一下action的代码:
package com.ming.user.action; import java.io.InputStream; import com.google.inject.Inject; import com.ming.core.util.WebUtils; import com.ming.user.entity.User; import com.ming.user.service.UserService; public class UserAction { private String account; private int user_id; private InputStream inputStream = null; @Inject private UserService userService; public String saveUser() throws Exception{ User u = new User(); u.setAccount(account); u.setUser_id(user_id); try { userService.add(u); inputStream=WebUtils.responseText(true, "保存信息成功!"); } catch (Exception e) { inputStream=WebUtils.responseText(false, "保存信息失败!"); e.printStackTrace(); } return "data"; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public int getUser_id() { return user_id; } public void setUser_id(int user_id) { this.user_id = user_id; } public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } }
struts2就是这样与guice整合成功的。
源码下载:喜欢我的代码点个赞呗
时间: 2024-10-13 05:51:54