SpringMVC 跳转时地址栏中的参数怎么隐藏
发布网友
发布时间:2022-05-14 07:23
我来回答
共1个回答
热心网友
时间:2023-08-11 10:28
在项目里,如果发生异常,我会需要重定向到一个指定的页面去告诉别人出问题了。这个时候一般我们都是用
response.sendRedirect(url?mesage=xxxx);这是GET方式的。如果我们要以POST方式重定向时,找了一下,发现
没有现成的东西,可以办得到。这里要自己写一个方法去实现:
httpClient.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author billtsang
*
*/
public class HttpClient {
Map<String, String> parameter=new HashMap<String, String>();
HttpServletResponse response;
public HttpClient(HttpServletResponse response)
{
this.response=response;
}
public void setParameter(String key,String value)
{
this.parameter.put(key, value);
}
public void sendByPost(String url) throws IOException
{
this.response.setContentType("text/html");
PrintWriter out = this.response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>sender</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<form name=\"submitForm\" action=\""+url+"\" method=\"post\">");
Iterator<String> it=this.parameter.keySet().iterator();
while(it.hasNext())
{
String key=it.next();
out.println("<input type=\"hidden\" name=\""+key+"\" value=\""+this.parameter.get(key)+"\"/>");
}
out.println("</from>");
out.println("<script>window.document.submitForm.submit();</script> ");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
在我们要跳转地方
HttpClient http=new HttpClient (response);
http.setParameter("message","xxxx");
http.sendByPost(url);