基础:结果视图

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-devMode" value="true"></constant>
    <package name="p1" extends="struts-default">
        <!-- 转发到页面 -->
        <action name="action1" class="com.learning.web.action.Demo1Action" method="say">
            <result name="success" type="dispatcher">/index.jsp</result>
        </action>
        
        <!-- 重定向到页面 -->
        <action name="action2" class="com.learning.web.action.Demo1Action" method="say">
            <result name="success" type="redirect">/index.jsp</result>
        </action>
        
        <!-- 转发到动作(同包) -->
        <action name="action3" class="com.learning.web.action.Demo1Action" method="say">
            <result name="success" type="chain">action1</result>
        </action>
        
        <!-- 重定向到动作(同包) -->
        <action name="action4" class="com.learning.web.action.Demo1Action" method="say">
            <result name="success" type="redirectAction">action1</result>
        </action>
        
        <!-- 转发到动作(不同包) -->
        
        <action name="action6" class="com.learning.web.action.Demo1Action" method="say">
            <result name="success" type="chain">
                <param name="namespace">/n2</param>
                <param name="actionName">action5</param>
            </result>
        </action>
        
            <!-- 重定向到动作(不同包) -->
        
        <action name="action7" class="com.learning.web.action.Demo1Action" method="say">
            <result name="success" type="redirectAction">
                <param name="namespace">/n2</param>
                <param name="actionName">action5</param>
            </result>
        </action>
        
    </package>
    
    <package name="p2" extends="struts-default" namespace="/n2">
        <action name="action5" class="com.learning.web.action.Demo1Action" method="say">
            <result name="success" type="dispatcher">/index.jsp</result>
        </action>
    </package>
    
    
    <!-- 定义全局结果视图集 -->
    <package name="mydefault" abstract="true" extends="struts-default">
        <result-types>
            <result-type name="CheckDemo" class="com.learning.web.action.MyResult"></result-type>
        </result-types>
        <global-results>
        <!-- 依赖注入思想 -->
            <result name="success" type="CheckDemo">
                <param name="width">400</param>
                <param name="height">300</param>
            </result>
        </global-results>
    </package>
    
    
    <!-- 先寻找局部的结果视图,没有在寻找全局结果视图 -->
    <package name="p3" extends="mydefault" namespace="/user">
        <action name="checkCode">
            
        </action>
    </package>
    
</struts>

 

实现自己的结果类型:继承 StrutsResultSupport

 

MyResult.java文件

 

package com.learning.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;

import com.opensymphony.xwork2.ActionInvocation;

import cn.dsna.util.images.ValidateCode;

public class MyResult  extends StrutsResultSupport{

    
    
    private int width;
    private int height;
    
    
    public void setWidth(int width) {
        this.width = width;
    }


    public void setHeight(int height) {
        this.height = height;
    }


    protected void doExecute(String arg0, ActionInvocation arg1) throws Exception {
        
    
        
        ValidateCode validateCode=new ValidateCode(width, height, 4, 9);
        
        //获得request和response
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        validateCode.write(response.getOutputStream());
        
    }

}

 

login.jsp文件

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'login.jsp' starting page</title>
  </head>
  <body>
  
      <form action="${pageContext.request.contextPath }/login.action" method="post">
              
          用户名:    <input type="text" name="username" /><br/>
          密码:    <input type="password" name="password" /><br/>
          验证码:    <input type="text" name="checkCode" /><img alt="" src="${pageContext.request.contextPath }/user/checkCode.action"><br/>
              <input type="submit" value="提交" /><br/>
      </form>
  
  </body>
</html>

 

posted @ 2017-04-10 13:48  第九种格调的人生  阅读(137)  评论(0编辑  收藏  举报