Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在struts2中以最少的代码更改更改post请求以获得重定向?_Java_Jsp_Struts2_Https_Post Redirect Get - Fatal编程技术网

Java 如何在struts2中以最少的代码更改更改post请求以获得重定向?

Java 如何在struts2中以最少的代码更改更改post请求以获得重定向?,java,jsp,struts2,https,post-redirect-get,Java,Jsp,Struts2,Https,Post Redirect Get,我的struts版本是2.3.15.3 在我当前的struts2项目中,所有post请求都不会重定向到get请求 因此,如果我使用https和chrome,当我单击“上一步”按钮时,我将收到重新提交警报 如何使用最小的努力来更改它们以保持使用post请求,但使用get重定向来显示JSP?我可以只修改struts.xml 或者,是否可以在没有PRG的情况下防止警报 这是我当前的代码 struts.xml: <action name="Get" class="test.PostAction"

我的struts版本是2.3.15.3

在我当前的struts2项目中,所有post请求都不会重定向到get请求

因此,如果我使用https和chrome,当我单击“上一步”按钮时,我将收到重新提交警报

如何使用最小的努力来更改它们以保持使用post请求,但使用get重定向来显示JSP?我可以只修改
struts.xml

或者,是否可以在没有PRG的情况下防止警报

这是我当前的代码


struts.xml

<action name="Get" class="test.PostAction">
    <result name="gotoform">/WEB-INF/jsp/PostForm.jsp</result>
    <result name="success">/WEB-INF/jsp/showGet.jsp</result>
</action>
public class PostAction  extends ActionSupport{
    @Override
    public String execute() throws Exception {

        Map para = ActionContext.getContext().getParameters();
        if(para.get("post")==null)
        {
            return "gotoform";
        }
        else
        {
            return "success";
        }
    }
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <form action="Get.action" method="POST">
            <input type="text" name="post" value="yes">
            <input type="submit">
        </form>
    </body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <h1>Display here</h1>
    </body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Display here, Post value=</h1> <%= request.getParameter("post") %>

    </body>
</html>
package test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;


public class PostAction  extends ActionSupport{

    private String post;

    @Override
    public String execute() throws Exception {

        Map para = ActionContext.getContext().getParameters();

        System.out.print("post = "+post);
        if(para.get("post")==null)
        {
            return "gotoform";
        }
        else
        {
            return "success";
        }
    }

    public String getPost() {
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Display here</h1>,
        <s:property value="post"/>,
        ${post},
        <%= post %>,
        <%= getPost() %>
    </body>
</html>
PostForm.jsp

<action name="Get" class="test.PostAction">
    <result name="gotoform">/WEB-INF/jsp/PostForm.jsp</result>
    <result name="success">/WEB-INF/jsp/showGet.jsp</result>
</action>
public class PostAction  extends ActionSupport{
    @Override
    public String execute() throws Exception {

        Map para = ActionContext.getContext().getParameters();
        if(para.get("post")==null)
        {
            return "gotoform";
        }
        else
        {
            return "success";
        }
    }
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <form action="Get.action" method="POST">
            <input type="text" name="post" value="yes">
            <input type="submit">
        </form>
    </body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <h1>Display here</h1>
    </body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Display here, Post value=</h1> <%= request.getParameter("post") %>

    </body>
</html>
package test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;


public class PostAction  extends ActionSupport{

    private String post;

    @Override
    public String execute() throws Exception {

        Map para = ActionContext.getContext().getParameters();

        System.out.print("post = "+post);
        if(para.get("post")==null)
        {
            return "gotoform";
        }
        else
        {
            return "success";
        }
    }

    public String getPost() {
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Display here</h1>,
        <s:property value="post"/>,
        ${post},
        <%= post %>,
        <%= getPost() %>
    </body>
</html>

新建
showGet.jsp

<action name="Get" class="test.PostAction">
    <result name="gotoform">/WEB-INF/jsp/PostForm.jsp</result>
    <result name="success">/WEB-INF/jsp/showGet.jsp</result>
</action>
public class PostAction  extends ActionSupport{
    @Override
    public String execute() throws Exception {

        Map para = ActionContext.getContext().getParameters();
        if(para.get("post")==null)
        {
            return "gotoform";
        }
        else
        {
            return "success";
        }
    }
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <form action="Get.action" method="POST">
            <input type="text" name="post" value="yes">
            <input type="submit">
        </form>
    </body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <h1>Display here</h1>
    </body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Display here, Post value=</h1> <%= request.getParameter("post") %>

    </body>
</html>
package test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;


public class PostAction  extends ActionSupport{

    private String post;

    @Override
    public String execute() throws Exception {

        Map para = ActionContext.getContext().getParameters();

        System.out.print("post = "+post);
        if(para.get("post")==null)
        {
            return "gotoform";
        }
        else
        {
            return "success";
        }
    }

    public String getPost() {
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Display here</h1>,
        <s:property value="post"/>,
        ${post},
        <%= post %>,
        <%= getPost() %>
    </body>
</html>
新建2
showGet.jsp

<action name="Get" class="test.PostAction">
    <result name="gotoform">/WEB-INF/jsp/PostForm.jsp</result>
    <result name="success">/WEB-INF/jsp/showGet.jsp</result>
</action>
public class PostAction  extends ActionSupport{
    @Override
    public String execute() throws Exception {

        Map para = ActionContext.getContext().getParameters();
        if(para.get("post")==null)
        {
            return "gotoform";
        }
        else
        {
            return "success";
        }
    }
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <form action="Get.action" method="POST">
            <input type="text" name="post" value="yes">
            <input type="submit">
        </form>
    </body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <h1>Display here</h1>
    </body>
</html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Display here, Post value=</h1> <%= request.getParameter("post") %>

    </body>
</html>
package test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;


public class PostAction  extends ActionSupport{

    private String post;

    @Override
    public String execute() throws Exception {

        Map para = ActionContext.getContext().getParameters();

        System.out.print("post = "+post);
        if(para.get("post")==null)
        {
            return "gotoform";
        }
        else
        {
            return "success";
        }
    }

    public String getPost() {
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Display here</h1>,
        <s:property value="post"/>,
        ${post},
        <%= post %>,
        <%= getPost() %>
    </body>
</html>

JSP页面
在这里展示,
,
${post},
,

公开
重定向操作
结果所需的附加操作


/WEB-INF/jsp/showGet.jsp
/WEB-INF/jsp/PostForm.jsp
表演

这是可行的,但是如果我需要jsp中的post数据呢?请查看新的showGet.jsp。如何获取post值?使用setter在action类中创建一个名为
post
的属性,以符合JavaBeans规范。该属性应为string类型,提交表单时应设置该属性。我添加了post属性,但无法在jsp上显示
System.out.print(“post=“+post”),我可以看到控制台输出是
INFO:post=null INFO:post=yes
,操作被调用了两次,第二次,值被设置为post,在jsp,我尝试了
,${post},
,但值没有显示我只是做了另一次尝试,如果我改回不重定向,我可以使用
${post}
来显示值,但是如果我使用重定向来显示值,我不知道如何获取值
post
您需要映射
showGet
操作的方法,在返回成功结果之前初始化属性。