Struts2 action中一个方法返回多个页面

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">很多时候,一个页面或不同页面中有多个form表单需要提交到action中的同一个方法中,这时,可在form表单提交的action后加参数。具体做法如下</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">方便起见,在同一个页面建两个form 标签,提交到同一action中相同的方法中。在提交的action后加上参数"flag"分别为1和2。</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="html"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  
  <body>
  
  	<!--flag参数值为1  -->
	<form action="Test_test.action?flag=1" method="post">
		<input type="submit" value="这是第一个action"/>
	</form>
	<br>
	<br>
	<!--flag参数值为2  -->
	<form action="Test_test.action?flag=2" method="post">
		<input type="submit" value="这是第二个action"/>
	</form>
  </body>
</html>



<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">接着创建一个Action</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);"></span></span><pre name="code" class="java">package action;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;


public class TestAction extends ActionSupport{
	//获得HttpServletRequest对象
	private HttpServletRequest request=ServletActionContext.getRequest();
	
	public String test()
	{
		//判断参数值
		if (request.getParameter("flag").equals("1")) {
			return "flag1";
		}
		else{
			return "flag2";
		}
	}

}



<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">配置struts.xml</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);"></span></span><pre name="code" class="html"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="test" namespace="/" extends="struts-default">
		<action name="*_*" class="action.{1}Action" method="{2}">
			<!--根据不同的返回值跳转到对应的jsp页面  -->
			<result name="flag1">/flag1.jsp</result>
			<result name="flag2">/flag2.jsp</result>
		</action>
	</package>
</struts>    



<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">根据struts.xml中配置再创建两个简单的JSP页面,还有web.xml中struts框架的配置这里就不在赘述,直接给出页面效果</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);"><img src="https://img-blog.csdn.net/20160703154924261?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);"><img src="https://img-blog.csdn.net/20160703154938901?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">这里可以看到浏览器地址栏中action后面参数flag的值</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);"><img src="https://img-blog.csdn.net/20160703155052420?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">
</span></span>

阅读更多
文章标签: Struts2 action
个人分类: Struts2
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭