面对不同种类的处理结果——<result type="">

Struts2中有很多不同种类的处理结果,有action也有视图文件。比较常见的情景就是我们要一个action链接到另一个action应该怎么做呢?我在这里就做了一个小例子

JSP文件内容



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<!-- chain链式结果类型,从一个action跳到另一个action -->
		<s:form action="start">
			<s:submit value="chain链式"/>
		</s:form>
		
		<!-- 源代码类型 -->
		<s:form action="plainText">
			<s:submit value="plainText源代码"/>
		</s:form>
		
		<!-- 请求转发 -->
		<s:form action="dispatcher">
			<s:submit value="dispatcher转发到JSP视图文件"/>
		</s:form>
		
		<!-- 请求重定向 -->
		<!-- 注意:重定向和请求转发可是有区别的哦,比较明显的是URL变化 -->
		<s:form action="redirect">
			<s:submit value="redirect重定向到JSP视图文件"/>
		</s:form>
		
		<!-- 请求重定向到一个action -->
		<s:form action="redirectAction">
			<s:submit value="redirectAction重定向到一个Action中"/>
		</s:form>
		
		<!-- 请求重定向,可以自定义的
		 ???这里只能是英文的,我就算在xml中设置的charSet也没有用,有知道的前辈求解一下这个问题,十分感谢-->
		<s:form action="redirect-custom">
			<s:textfield name="myUrl"/>
			<s:submit/>
		</s:form>
		
		<!-- 测试全局结果,如果一个action找不到匹配 -->
		<s:form action="login!global">
			<s:submit value="TestGlobal"/>
		</s:form>
	</body>
</html>

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="true" />
    <constant name="struts.devMode" value="true" />
	<constant name="struts.custom.i18n.resources" value="test"/>

    <package name="default" namespace="/" extends="struts-default">
		
		<global-results>
			<result name="TestGlobal">/content/TestGlobal.jsp</result>
		</global-results>
		
		<action name="login" class="action.LoginAction">
			<result name="success">/content/success.jsp</result>
		</action>
		
		<action name="start" class="action.ActionOne">
			<result type="chain">end</result>
		</action>
		<action name="end" class="action.ActionTwo">
			<result>/content/success.jsp</result>
		</action>
		
		<action name="plainText">
			<result type="plainText">
				<param name="location">/content/success.jsp</param>
				<param name="charSet">UTF-8</param>
			</result>
		</action>
		
		<action name="dispatcher">
			<result type="dispatcher">/content/success.jsp</result>
		</action>
		
		<action name="redirect">
			<result type="redirect">/content/success.jsp</result>
		</action>
		
		<action name="redirectAction">
			<result type="redirectAction">
				<param name="actionName">TestRedirectAction</param>
				<param name="namespace">/</param>
			</result>
		</action>
		
		<action name="redirect-custom" class="action.RedirectCustom">
			<result type="redirect">
				<param name="location">testRedirect.action?myUrl=${myUrl}</param>
			</result>
		</action>
		
		<action name="testRedirect" class="action.TestRedirect">
			<result>/content/success.jsp</result>
		</action>
		
		<action name="TestRedirectAction">
			<result>/content/success.jsp</result>
		</action>
			
    </package>

</struts>
action中的类的内容

ActionOne.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class ActionOne extends ActionSupport {
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("This is ActionOne");
		return super.execute();
	}
}



ActionTwo.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class ActionTwo extends ActionSupport {
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("This is ActionTwo");
		return super.execute();
	}
}



LoginAction.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("This is execute");
		return super.execute();
	}
	
	public String method1() throws Exception {
		System.out.println("This is one");
		return SUCCESS;
	}
	public String method2() throws Exception {
		System.out.println("This is two");
		return SUCCESS;
	}
	public String global() throws Exception {
		System.out.println("This is glboal()");
		return "TestGlobal";
	}
}



RedirectCustom.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class RedirectCustom extends ActionSupport {
	private String myUrl;

	public String getMyUrl() {
		return myUrl;
	}

	public void setMyUrl(String myUrl) {
		this.myUrl = myUrl;
	}
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("这里是RedirectCustom的execute(),接收到的myUrl为" + getMyUrl());
		return super.execute();
	}
}



TestRedirect.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class TestRedirect extends ActionSupport {
	private String myUrl;
	
	public String getMyUrl() {
		return myUrl;
	}

	public void setMyUrl(String myUrl) {
		this.myUrl = myUrl;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("这里是TestRedirect的execute(),接收到的myUrl值为:" + getMyUrl());
		return super.execute();
	}
}
呼呼~总之你做好这几个文件就好了,之后慢慢看action是如何跳转的。我先撤了

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:白松林 返回首页