首页 > 代码库 > 基础:结果视图
基础:结果视图
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 src="http://www.mamicode.com/${pageContext.request.contextPath }/user/checkCode.action"><br/> <input type="submit" value="http://www.mamicode.com/提交" /><br/> </form> </body> </html>
基础:结果视图
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。