struts2结果类型分析

本文探讨了在Web开发中采用请求转发方式处理表单提交可能导致的数据重复问题,并提出使用重定向作为解决方案。同时,通过Struts框架示例介绍了如何在不同Action间传递参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

采用请求转发的方式完成表单内容的插入会造成表单的数据的重复插入。

}else if (path.equals("/add")) {
					
					try {
						Emp emp = this.getEmp(request);
						dao.addEmp(emp);
						request.getRequestDispatcher("list.do").forward(request, response);
						//response.sendRedirect("list.do");
					} catch (Exception e) {
						response.sendRedirect("login.jsp");
						//e.printStackTrace();
					}

当用户提完数据,提交后,由于用的是请求转发到list.do。都是在一个请求中。用户看到listEem.jsp后,此时浏览器的地址栏认识add.do,当用户刷新的时候就会重复的加入添加数据。


解决方法:

try {
						Emp emp = this.getEmp(request);
						dao.addEmp(emp);
						//request.getRequestDispatcher("list.do").forward(request, response);
						response.sendRedirect("list.do");
					} catch (Exception e) {
						response.sendRedirect("login.jsp");
						//e.printStackTrace();
					}

使用重定向后,用户的浏览器地址栏是list.do不管怎么刷新都不会出现重复添加数据的情况,因为不是在一个请求中。

struts中result标签中type属性常用的有:dispatcher ,redirect,redirectAction

redirectAction DEMO

action1.jsp

<%@ 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 'action1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    	<form action = "action1.action">
    		username:<input type = "text" name = "username"><br/>
    		password:<input type = "password" name = "password"/><br/>
    		<input type = "submit" value = "submit" />
    	</form>
  </body>
</html>

action1.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class Action1 extends ActionSupport{
	
	
	@Override
	public String execute() throws Exception {
		
		return SUCCESS;
	}
}

action2.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class Action2 extends ActionSupport {
		
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
}

action2.jsp

<%@ 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 'action2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    	<h1>action2.jsp</h1>
  </body>
</html>

struts.xml

			<action name = "action1" class = "action.Action1">
				<result name = "success" type = "redirectAction">action2</result>
			</action>
			<action name = "action2" class = "action.Action2">
				<result name = "success">/action2.jsp</result>
			</action>

传递参数:


action1.jsp

<%@ 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 'action1.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    	<form action = "action1.action">
    		username:<input type = "text" name = "username"><br/>
    		password:<input type = "password" name = "password"/><br/>
    		<input type = "submit" value = "submit" />
    	</form>
  </body>
</html>

Action1.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class Action1 extends ActionSupport{
	
	private String username;
	private String password;
	
	private String usernameAndpassword;
	
	
	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getUsernameAndpassword() {
		return usernameAndpassword;
	}


	public void setUsernameAndpassword(String usernameAndpassword) {
		this.usernameAndpassword = usernameAndpassword;
	}


	@Override
	public String execute() throws Exception {
		this.usernameAndpassword = this.username+":"+this.password;
		return SUCCESS;
	}
}

Action2.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class Action2 extends ActionSupport {
	private String username;
	
	private String password;
	
	private String usernameAndpassword;
	
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUsernameAndpassword() {
		return usernameAndpassword;
	}
	public void setUsernameAndpassword(String usernameAndpassword) {
		this.usernameAndpassword = usernameAndpassword;
	}
	@Override
	public String execute() throws Exception {
		
		return SUCCESS;
	}
}

action2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri = "/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    
    <title>My JSP 'action2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    	<h1>username:<s:property value = "username"/></h1>
    	<h1>password:<s:property value = "password"/></h1>
    	<h1>usernameAndpassword:<s:property value = "usernameAndpassword"/></h1>
  </body>
</html>
struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<package name = "sss" extends="struts-default" namespace="/">
			<global-results>
				<result name="usernameInvalid" >/usernameInvalid.jsp</result>
				<result name = "passwordInvalid">/passwordInvalid.jsp</result>
			</global-results>
			<global-exception-mappings>
				<exception-mapping result="usernameInvalid" exception="exception.UsernameException"></exception-mapping>
				<exception-mapping result="passwordInvalid" exception="exception.PasswordException"></exception-mapping>
			</global-exception-mappings>
			<action name="UserAction2" class="action.UserAction2">
			</action>
			<action name = "login" class="action.LoginAction">
				<result name = "success" type = "redirect">/result.jsp</result>
				<result name = "input">/login.jsp</result>
			</action>
			<action name = "register" class = "action.RegisterAction">
				<result name = "success">/registerResult.jsp</result>
				<result name = "input">/regist.jsp</result>
			</action>
			<action name = "action1" class = "action.Action1">
				<result name = "success" type = "redirectAction">
					<param name="actionName">action2</param>
					//引用Action1中的变量
					<param name="username">${username}</param>
					<param name="password">${password}</param>
					<param name="usernameAndpassword">${usernameAndpassword}</param>
				</result>
				
			</action>
			<action name = "action2" class = "action.Action2">
				<result name = "success">/action2.jsp</result>
			</action>
	</package>
</struts>


type为chain的情况:

处理配置文件改动,其他都一样。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<package name = "sss" extends="struts-default" namespace="/">
			<global-results>
				<result name="usernameInvalid" >/usernameInvalid.jsp</result>
				<result name = "passwordInvalid">/passwordInvalid.jsp</result>
			</global-results>
			<global-exception-mappings>
				<exception-mapping result="usernameInvalid" exception="exception.UsernameException"></exception-mapping>
				<exception-mapping result="passwordInvalid" exception="exception.PasswordException"></exception-mapping>
			</global-exception-mappings>
			<action name="UserAction2" class="action.UserAction2">
			</action>
			<action name = "login" class="action.LoginAction">
				<result name = "success" type = "redirect">/result.jsp</result>
				<result name = "input">/login.jsp</result>
			</action>
			<action name = "register" class = "action.RegisterAction">
				<result name = "success">/registerResult.jsp</result>
				<result name = "input">/regist.jsp</result>
			</action>
			<action name = "action1" class = "action.Action1">
				<result name = "success" type = "chain">
					<param name="actionName">action2</param>
				</result>
				
			</action>
			<action name = "action2" class = "action.Action2">
				<result name = "success">/action2.jsp</result>
			</action>
	</package>
</struts>


如果在action1.jsp中输入:

zhangsan

helloworld

在action中结果是

username:zhangsan

password:helloworld

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值