采取请求转发的方式(dispatcher,默认情况)完成表单内容的添加会造成内容的重复插入;而采取重定向的方式(redirectAction)实现数据的添加不会导致数据的重复插入
result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
username: ${requestScope.username }
password: ${requestScope.password }
age: ${requestScope.age }
date: ${requestScope.date }
session:${sessionScope.hello }
注意由于request和session的区别,redirect方式时不显示
“ username: ${requestScope.username }
password: ${requestScope.password }
age: ${requestScope.age }
date: ${requestScope.date }
”
而dispatcher则全显示.
redirectAction方式:携带参数传递 (重要)
action1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
>
struts.xml
action2
${username}
${password}
${usernameAndPassword}
/action2.jsp
Action1.java
package com.shengsiyuan.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class Action1 extends ActionSupport
{
private String username ;
private String password ;
private String usernameAndPassword ;
public String getUsernameAndPassword() {
return usernameAndPassword;
}
public void setUsernameAndPassword(String usernameAndPassword) {
this.usernameAndPassword = 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;
}
@Override
public String execute() throws Exception {
this.usernameAndPassword= this.username + this.password ;
return SUCCESS ;
}
}
Action2.java
package com.shengsiyuan.struts2;
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="ISO-8859-1"%>
< prefix = "s" uri = "/struts-tags" %>
username:
password:
usernameandpassword
chain类型:
若将以上改为chain 即
action2
${username}
${password}
${usernameAndPassword}
/action2.jsp
则在最终显示jsp页面只显示username 和password,不现实usernameAndPassword内容。即以下内容只与redirectAction方式有关,与chain等方式无关:(即仅为redirecAction的参数)
${username}
${password}
${usernameAndPassword}
防止表单重复提交有2种方式:
1通过重定向
2通过Session Token(Session令牌)
阅读(1156) | 评论(0) | 转发(0) |