Spring是如何管理Hibernate和Struts的(二)
将Struts2交给Spring管理的时候需要加入一个Spring-struts.jar包
Struts2的运行依赖于容器, 所以tomcat启动时会加载在web.xml中的配置文件.
<filter> <filter-name>struts2> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-mapping>/*</url-mapping> </filter-mapping>
这样, tomcat启动的时候,会管理struts2, 并加载classpath下的struts.xml文件.
struts.xml中的常量配置.
struts.action.extension 扩展名,默认为.action
struts.devMode 开发模式
struts.custom.i18n.resources=resource resource_zh_CN.properties resource_en_US.properties
struts.i18n.encoding 编码格式 struts.ui.theme 自定义主题
struts.xml中包的配置.
<package name="test" namespace="/" extends="struts-default"> </package>
package包含在struts标签中, 一个struts标签中可以有多个package. 每个包都有一个唯一的name, 和一个namespace. extends表示继承于哪个包, 并且允许多继承.
在package中配置interceptor, globleResults, globle-exception-mappings
1: 在package中配置interceptor
<interceptors> <interceptor name="loginInterceptor" class="org.test.LoginInterceptor" ></interceptor> <interceptor-stack name="default"> <interceptor-ref name="defaultStack" ></interceptor-ref> <interceptor-ref name="loginInterceptor"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="default"></default-interceptor-ref>
说明一下, 首先在package中的interceptors标签内声明一个interceptor , name属性必须是唯一的.
然后在<interceptor-stack>中配置<interceptor-ref> 在配置前必须声明defaultstack.
当配置好interceptor-stack后, 需要配置缺省的interceptor
<default-interceptor-ref name="default" ></default-interceptor-ref>
2: 配置globle-results
<globle-results> <result name="list" type="redirect"></result> <result name="save" type="redirect-action"></result> </globle_results>
默认的retult type是dispatcher.
Struts2自定义返回类型.
import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.Result; import com.tiros.caredog.cd.action.ImageAction; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; /** * image Result * @author songzj * */ public class ImageResult implements Result { public void execute(ActionInvocation ai) throws Exception { ImageAction action = (ImageAction) ai.getAction(); HttpServletResponse response = ServletActionContext.getResponse(); response.setHeader("Cash", "no cash"); response.setContentType(action.getContentType()); response.setContentLength(action.getContentLength()); response.getOutputStream().write(action.getImageBytes()); //response.getOutputStream().flush(); response.getOutputStream().close(); } }
<result-types> <result-type name="ValidateImage" class="com.tiros.caredog.cd.util.ImageResult" /> </result-types>
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.util.Random; import com.tiros.caredog.cd.util.Constent; public class ImageAction extends CommonAction { /** * * 用于随机生成验证码的数据源 * */ private static final char[] source = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; /** * * 用于随机打印验证码的字符颜色 * */ private static final Color[] colors = new Color[] { Color.RED, Color.BLUE, Color.BLACK }; /** * * 用于打印验证码的字体 * */ private static final Font font = new Font("宋体", Font.ITALIC, 15); /** * * 用于生成随机数的随机数生成器 * */ private static final Random rdm = new Random(); private String text = ""; private byte[] bytes = null; private String contentType = "image/png"; public byte[] getImageBytes() { return this.bytes; } public String getContentType() { return this.contentType; } public void setContentType(String value) { this.contentType = value; } public int getContentLength() { return bytes.length; } /** * * 生成长度为4的随机字符串 * */ private void generateText() { char[] source = new char[4]; for (int i = 0; i < source.length; i++) { source[i] = ImageAction.source[rdm .nextInt(ImageAction.source.length)]; } this.text = new String(source); // 设置Session super.getSession().setAttribute(Constent.VALIDATE_CODE_KEY, this.text); } /** * * 在内存中生成打印了随机字符串的图片 * * @return 在内存中创建的打印了字符串的图片 * */ private BufferedImage createImage() { int width = 40; int height = 15; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); g.setFont(font); for (int i = 0; i < this.text.length(); i++) { g.setColor(colors[rdm.nextInt(colors.length)]); g.drawString(this.text.substring(i, i + 1), 2 + i * 8, 12); } g.dispose(); return image; } /** * * 根据图片创建字节数组 * * @param image * 用于创建字节数组的图片 * */ private void generatorImageBytes(BufferedImage image) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { javax.imageio.ImageIO.write(image, "jpg", bos); this.bytes = bos.toByteArray(); } catch (Exception ex) { } finally { try { bos.close(); } catch (Exception ex1) { } } } /** * * 被struts2过滤器调用的方法 * * @return 永远返回字符串"image" * */ public String doDefault() { this.generateText(); BufferedImage image = this.createImage(); this.generatorImageBytes(image); return "image"; } }
<action name="va_ValidateImage" class="ImageAction" method="doDefault"> <result name="image" type="ValidateImage" /> </action>
<!-- 验证码管理 --> <bean id="ImageAction" class="com.cd.action.ImageAction" scope="prototype"> </bean>
在上面的代码中我们可以发现, struts.xml中定义的action name ="va_validateImage" class="ImageAction"
这个ImageAction从哪里来呢? 从applicationContext.xml中来. 因为Spring管理了所有的Bean, 所以ImageAction会交由Spring管理. 当用到ImageAction的时候会直接从Spring的工厂bean中得到.