你知道任何实用程序类/库,可以将Map转换为URL友好的查询字符串吗?
示例:
我有一张地图:
- "param1"= 12,
- "param2"="cat"
我想得到:__CODE__
.
PS.我知道我可以轻松地自己编写,我很惊讶我无法在任何地方找到它(我到目前为止检查了Apache Commons).
我想在我的参数图中添加一个新参数HttpServletRequest
.
以下代码
request().getParameterMap().put("j_username", user);
request().getParameterMap().put("j_password", pwd);
Run Code Online (Sandbox Code Playgroud)
创建此错误
no modifications are allowed to a locked parameter map
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
我很难相信我需要从请求参数中修剪前导空格和尾随空格并不是一个常见的要求,但我似乎找不到任何能够很好地解决它的人.
当然有一些简单的方法来配置,比如Spring WebDataBinder
在将它们映射到目标对象之前从参数值中修剪所有空格?
我真的很想知道其他人是如何解决这个问题的.
我正在使用子类HttpServletRequestWrapper
对请求参数进行一些翻译,并在第一次请求时缓存翻译的值.例如,第一次getQueryString()
被调用,我调用super.getQueryString()
并计算我想要的结果并将其保存在字段中,然后返回它.下次,我只使用缓存的结果.
除非有一些"转发",否则这种方法就像魅力一样.转发请求时,Tomcat会替换原始请求,因此我的缓存查询字符串不会更改,转发的页面将获取原始查询字符串,而不是转发到的字符串.
覆盖setRequest()
清除缓存的方法也无济于事,好像请求被包装两次一样,它调用setRequest
内包装器(不是我的),我无法知道它何时发生.
我正在寻找一种在包装的请求层次结构发生变化时得到通知的方法,以便在有"转发"时我可以清除缓存.
我需要创建一个过滤器并修改请求对象中设置的标头值。我们如何使用Filter修改请求对象中的标头?请求对象中没有可用的setHeader方法。
我想在My REST Web Service Project中记录请求正文的所有请求和消息.我使用Spring和json消息传送器.但我面对java.lang.IllegalStateException:已经为此请求调用了getReader().我怎么解决?或者还有另一种方式吗?
web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>LogFilter</filter-name>
<filter-class>com.cmpolaris.system.LogFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LogFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
弹簧beans.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller …
Run Code Online (Sandbox Code Playgroud) 我觉得这应该很容易。我有一个应用程序,我想要做的就是有一个表单页面 (index.jsp),它调用一个 servlet (CheckInfo.java),它设置一个新的标头 (myHeader) 并将用户重定向到另一个页面 (redirect.jsp) . 所有这些文件都在同一台服务器上。index.jsp 正在发送请求,CheckInfo 正在处理和重定向,但 myHeader 没有显示在 redirect.jsp 上。我读过几篇关于 response.sendRedirect 发送不传递标头的 302 的帖子,我应该使用 RequestDispatcher,但似乎没有任何效果。有没有办法将头从 servlet 发送到 jsp?这是servlet代码:
response.setHeader("myHeader", "hey there");
response.sendRedirect("redirect.jsp");
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
response.setHeader("myHeader", "hey there");
RequestDispatcher view = request.getRequestDispatcher("redirect.jsp");
view.forward(request, response);
Run Code Online (Sandbox Code Playgroud)
我在redirect.jsp中有这个:
System.out.println(request.getHeader("myHeader"));
Run Code Online (Sandbox Code Playgroud)
这不会打印任何内容。
如果我的问题的答案是否定的……那么一旦我回到 jsp,我就会找到一种设置标题的方法。我的反向代理正在寻找特定的标头以确定是否执行操作。显然,我在redirect.jsp 上尝试了response.addHeader(),但此时页面已经加载,这让我觉得自己很傻。
java ×6
servlets ×4
spring ×2
data-binding ×1
forwarding ×1
http-headers ×1
java-ee ×1
redirect ×1
spring-mvc ×1
tomcat ×1
utilities ×1
web-services ×1
wrapper ×1