我有简单的JSF应用程序。并尝试了解UrlRewriteFilter。
首先我有以下网址: 的 http://testSite/autocomplete?action=lookup&id=45
我希望获得params 操作和 id 但同时我需要用户(和google-bot)从未见过这种类型的网址但总是 http://testSite/lookup/45
我重写了url,但无法向parat-controller发送params。请帮忙。
这是web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- URL ReWriter -->
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>AutoCompleteServlet</servlet-name>
<servlet-class>com.ajax.AutoCompleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AutoCompleteServlet</servlet-name>
<url-pattern>/autocomplete</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
urlrewrite.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite use-query-string="true">
<rule>
<from>/index.jsp</from>
<to type="redirect">%{context-path}/</to>
</rule>
<rule>
<from>^/autocomplete\?action=lookup&id=(.*)</from>
<!-- <run class="com.ajax.AutoCompleteServlet" method="doGet" />-->
<to type="redirect">%{context-path}/lookup/$1</to>
</rule>
<rule>
<from>^/lookup/(.*)</from>
<run class="com.ajax.AutoCompleteServlet" method="doPost">
<init-param>
<param-name>action</param-name>
<param-value>lookup</param-value>
</init-param>
<init-param>
<param-name>id</param-name>
<param-value>$1</param-value>
</init-param>
</run>
</rule>
</urlrewrite>
和Servlet:
package com.ajax;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
public class AutoCompleteServlet extends HttpServlet {
private ServletContext context;
private ComposerData compData = new ComposerData();
private HashMap composers = compData.getComposers();
@Override
public void init(ServletConfig config) throws ServletException {
this.context = config.getServletContext();
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String action = request.getParameter("action"); // Always null ???
String targetId = request.getParameter("id"); // Always null ????
}
}
答案 0 :(得分:1)
这确实是预期的行为。您正在重定向到路径信息中包含这些参数的URL,而不是查询字符串。
您基本上有两种选择:
添加执行转发的规则(无重定向!),另请参阅&#34;清理网址&#34;在documentation。
<rule>
<from>^/lookup/(.*)$</from>
<to>/autocomplete?action=lookup&id=$1</to>
</rule>
更改servlet以从路径信息中获取这些参数。
String[] params = request.getPathInfo().substring(1).split("/");
String action = params[0]; // "lookup"
String id = params[1]; // "45"
// ...
无关具体问题,如果&#34;脏&#34;网址已经预先发布到网络中,并且已经被搜索网站编入索引,您最好使用<to type="redirect-permanent">
(HTTP 301)代替<to type="redirect">
(HTTP 302)。这将指示搜索机器人从索引中删除URL,而不是保留为备用URL。