urlrewriter中怎么将解析伪静态后的参数重新放到request里面?
如题, 我把FakeStaticHandler改了下, 可以根据类似urlrewriter的参数配置实现伪静态, 但是不清楚怎么做可以最高效的把从伪静态地址中解析出的参数重新放到request里面。
解决办法我这里也想了两个, 一个是在handler里面虚拟一个form, 重新提交url, 另一个是通过HttpClient重新提交, 但这两种办法都破坏了handler的结构, 感觉很不好, 请教下有没有什么优雅、高效的办法来解决这个问题?
代码如下:
public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) { /*if ("/".equals(target)) { nextHandler.handle(target, request, response, isHandled); return; }*/ // pass static files String[] fakeDisallowStart = Constants.config.getStringArray("fake.disallow.start"); for(String disallow : fakeDisallowStart) { if(target.startsWith(disallow)) { System.out.println("target=" + target + ", disallow="+disallow); nextHandler.handle(target, request, response, isHandled); return; } } String[] fakeDisallowEnd = Constants.config.getStringArray("fake.disallow.end"); for(String disallow : fakeDisallowEnd) { if(target.endsWith(disallow)) { System.out.println("target=" + target + ", disallow="+disallow); nextHandler.handle(target, request, response, isHandled); return; } } /*if (target.indexOf('.') == -1) { HandlerKit.renderError404(request, response, isHandled); return ; } int index = target.lastIndexOf(viewPostfix); if (index != -1) target = target.substring(0, index);*/ // deal parameter // list_cid, list_cid_pageNumber, show_id List<FakeBoundModel> urlList = Constants.fake.getOutBoundList(); String real = "", fake = ""; for(FakeBoundModel url : urlList) { //^/article/list?cid=(d+)(;jsessionid=.*)?$ real = url.getReal(); ///article/list_{}.html fake = url.getFake(); List<String> values = RegexUtils.getValues(target, fake); if(values != null && values.size() > 0) { while(real.indexOf("{}") > 0 && (values != null && values.size() > 0)) { real = real.replace("{}", values.get(0)); values.remove(0); } target = real; } } if(target.indexOf("?") > 0) { // get query string, such as: a=a1&b=b1 String paramStr = target.substring(target.indexOf("?") + 1); // get param array, such as: [a=a1],[b=b1] String[] params = paramStr.split("&"); for(String p : params) { // get param name and value, such as: [a],[a1] String[] pp = p.split("="); if(pp.length == 2) { // TODO set parameter to request request.setAttribute(pp[0], pp[1]); } } target = target.substring(0, target.indexOf("?")); } nextHandler.handle(target, request, response, isHandled); }
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!-- outBound用于将外部伪静态网址转换为动态网址, inBound用于将动态网址转换为伪静态地址 --> <fakes> <inBound title="文章列表页-无分页"> <real>^/article/list?cid=(d+)(;jsessionid=.*)?$</real> <fake>/article/list_{}.html</fake> </inBound> <outBound title="文章列表页-无分页"> <real>/article/list?cid={}</real> <fake>^/article/list_(d+).html(;jsessionid=.*)?$</fake> </outBound> <!-- <inBound></inBound> --> <inBound title="文章列表页-带分页"> <real>^/article/list?cid=(d+)&pageNumber=(d+)(;jsessionid=.*)?$</real> <fake>/article/list_{}_{}.html</fake> </inBound> <outBound title="文章列表页-带分页"> <real>/article/list?cid={}&pageNumber={}</real> <fake>^/article/list_(d+)_(d+).html(;jsessionid=.*)?$</fake> </outBound> <inBound title="文章内容页"> <real>^/article/show?id=(d+)(;jsessionid=.*)?$</real> <fake>/article/show_{}.html</fake> </inBound> <outBound title="文章内容页"> <real>/article/show?id={}</real> <fake>^/article/show_(d+).html(;jsessionid=.*)?$</fake> </outBound> </fakes>
package org.yi.core.model; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "fakes") @XmlType public class FakeModel { // @XmlElementWrapper(name = "url") @XmlElement(name = "outBound") List<FakeBoundModel> outBoundList; @XmlElement(name = "inBound") List<FakeBoundModel> inBoundList; public List<FakeBoundModel> getOutBoundList() { return outBoundList; } public void setOutBoundList(List<FakeBoundModel> outBoundList) { this.outBoundList = outBoundList; } public List<FakeBoundModel> getInBoundList() { return inBoundList; } public void setInBoundList(List<FakeBoundModel> inBoundList) { this.inBoundList = inBoundList; } }
package org.yi.core.model; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement @XmlType(propOrder = {"real", "fake"}) public class FakeBoundModel { @XmlAttribute private String title; // 实际的动态网址 @XmlElement(required = true) private String real; // 伪静态地址 @XmlElement(required = true) private String fake; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getReal() { return real; } public void setReal(String real) { this.real = real; } public String getFake() { return fake; } public void setFake(String fake) { this.fake = fake; } }
@Override public void afterJFinalStart() { String xml = FileUtils.readFile(FileUtils.locateAbsolutePathFromClasspath("fake.xml")); org.yi.core.common.Constants.fake = FakeUtils.convery2Model(xml, FakeModel.class); super.afterJFinalStart(); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
轮子不好造, 已换为urlrewriter。
中途还使用了 form提交,可以确定的是把问题搞复杂了,建议要么使用 jfinal 的伪静态类似的 Handler,要么使用 urlrewrite 这样的外部优伪静态工具,组合起来用只会把事情搞得更加复杂。
mark,