struts2自定义结果类型

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>
	<!-- 
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>

    <include file="example.xml"/>
	 -->
    <!-- Add packages here -->
	<package name="default" namespace="/hello/xxx" extends="struts-default">
		<result-types>
			 <result-type name="captcha" class="com.itheima.results.CaptchaResult"/>
		</result-types>
		 <!--<action name="sayHello" class="com.itheima.actions.HelloAction" method="sayHello">
			<result name="success" type="dispatcher">/1.jsp</result>
		</action> -->
		<action name="captcha">
			<result name="success" type="captcha"></result>
		</action>
	</package>
</struts>

type="captcha" 对应的是上面声明过的 result-type name="captcha"

 

CaptchaResult:

package com.itheima.results;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
//自定义结果类型
public class CaptchaResult implements Result {
	//输出captcha图像
	public void execute(ActionInvocation invocation) throws Exception {
		
		HttpServletResponse response = ServletActionContext.getResponse();
		//图片不要缓存
		response.setHeader("Expires", "-1");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		
		BufferedImage image = CaptchaUtil.genImage();//得到图片
		
		ImageIO.write(image, "jpeg", response.getOutputStream());
		
		//让程序继续往下走
		invocation.invoke();
		
	}

}

 

Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>用户登陆</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	

  </head>
  
  <body>
    	<form action="">
    		用户名:<input type="text" name="username"/><br/>
    		验证码:<img src="${pageContext.request.contextPath}/hello/xxx/captcha.action"/><br/>
    		<input type="submit" value="登陆"/>
    	</form>
  </body>
</html>

  

posted @ 2015-07-31 09:49  vaer  阅读(316)  评论(0编辑  收藏  举报