[jboss-user] [JBoss Seam] - Re: Searchengine friendly links

spambob do-not-reply at jboss.com
Sat Nov 25 11:54:07 EST 2006


Now I don't understand it too - a classic case of 'I don't see the forest cause of all the trees'. Thanks for your clarification & patience!

EDIT: i just saw normans post while editing mine - so i post my solution, feel free to integrate / modify it:

The filterclass:
public class UrlRewriteFilter implements Filter {
  | 
  | 	private FilterConfig filterConfig;
  | 	private String[] variableNames;
  | 	private String[] delimiters;
  | 	private String targetUrl;
  | 
  | 	public void init(FilterConfig filterConfig) throws ServletException {		
  | 		this.filterConfig = filterConfig;
  | 		this.targetUrl = filterConfig.getInitParameter("target");
  | 		String pattern = "#\\{[^}]+}";
  | 		String configString = filterConfig.getInitParameter("pattern");
  | 		Pattern p = Pattern.compile(pattern);
  | 		Matcher m = p.matcher(configString);
  | 
  | 		List<String> variableNames = new ArrayList<String>();
  | 		while(m.find()) {
  | 			variableNames.add(configString.substring(m.start()+2, m.end()-1));
  | 		}
  | 		this.variableNames = variableNames.toArray(new String[variableNames.size()]);
  | 		this.delimiters = configString.split(pattern);
  | 	}
  | 
  | 	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  | 		HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
  | 		String request = httpRequest.getRequestURI();
  | 		
  | 		int start;
  | 		int end = request.indexOf(delimiters[0]);
  | 		for(int i=0; i<variableNames.length; i++) {
  | 
  | 	                start = end + delimiters[ i ].length();
  | 			end = request.indexOf(delimiters[i+1], start);
  | 			servletRequest.setAttribute(variableNames, request.substring(start, end));
  | 		}
  | 		filterConfig.getServletContext().getRequestDispatcher(targetUrl).forward(servletRequest, servletResponse);
  | 	}
  | 
  | 	public void destroy() {;
  | 
  | }

The config in web.xml:
<filter>
  |     	<filter-name>UrlRewriteFilter</filter-name>
  |     	<filter-class>com.example.UrlRewriteFilter</filter-class>
  |     	<init-param>
  |     		<param-name>pattern</param-name>
  |     		<param-value>/products/#{categoryId}/#{productId}.xhtml</param-value>
  |     	</init-param>
  |     	<init-param>
  |     		<param-name>target</param-name>
  |     		<param-value>/showProduct.xhtml</param-value>
  |     	</init-param>
  |     </filter>
  |     
  |     <filter-mapping>
  |     	<filter-name>UrlRewriteFilter</filter-name>
  |     	<url-pattern>/products/*</url-pattern>
  |     </filter-mapping>

The bean that loads the product is the one above and the jsf is trivial.

With a configuration like this a URL like http://www.example.com/products/cars/123.xhtml is mapped to http://www.example.com/showProducts.xhtml?categoryId=cars&productId=123 Note that the request variables are attributes not parameteres therefore @RequestParameter doesn't work.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3988547#3988547

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3988547



More information about the jboss-user mailing list