zoukankan      html  css  js  c++  java
  • 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>
    

      

  • 相关阅读:
    一 基础--进制转化
    七牛云上传视频并截取第一帧为图片(js实现)
    FNScanner二维码接口openView自定义扫码Demo
    UIPickerView 模块示例demo
    vPlayer 模块Demo
    doT的高级用法及loadData的使用
    acmPush模块示例demo
    UIChatBox模块示例demo
    分享一款基于aui框架的图文发布界面
    基于js的APP多语言处理
  • 原文地址:https://www.cnblogs.com/vaer/p/4691293.html
Copyright © 2011-2022 走看看