论坛首页 Web前端技术论坛

我做的UrlRewrite

浏览 2746 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-27   最后修改:2010-01-27

 xml文件:

 

<!-- UrlRewrite start -->
	<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>
	</filter-mapping>
	<!-- UrlRewrite end -->

<servlet>
    <servlet-name>NewsServlet2</servlet-name>
    <servlet-class>fkw.bll.news.NewsServlet2</servlet-class>
  </servlet>
<servlet-mapping>
    <servlet-name>NewsServlet2</servlet-name>
    <url-pattern>/servlet/NewsServlet2</url-pattern>
  </servlet-mapping>

 

urlrewrite.xml文件:

 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
        "http://tuckey.org/res/dtds/urlrewrite2.6.dtd">

<urlrewrite>

	<!-- 将/front/news/index.jsp映射为/news/index.html -->
	<rule>
		<from>/news/index.html</from>
		<to type="forward">/front/news/index.jsp</to>
	</rule>

	<!-- 将servlet/NewsServlet2?action=listNews伪装成news/listNews.html -->
	<rule>
		<from>news/listNews.html</from>
		<to type="forward">servlet/NewsServlet2?action=listNews</to>
	</rule>

	<!-- 讲servlet/NewsServlet2?action=getNews伪装成news/showNews.html -->
	<rule>
		<from>news/showNews_([0-9]+).html$</from>
		<to type="forward">
			servlet/NewsServlet2?action=getNews&amp;newsid=$1
		</to>
	</rule>

</urlrewrite>

 

工程目录结构:

 

 

Servlet:

 

package fkw.bll.news;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fkw.iface.INews;
import fkw.impl.ImplFactory;
import fkw.impl.news.NewsImpl;
import fkw.model.news.NewsBean;

/**
 * UrlRewrite test
 * 
 * @author saebalee
 * 
 */
public class NewsServlet2 extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2934173725340619559L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		System.out.println("新闻访问的action是:" + action);
		String forward = null;
		if ("listNews".equals(action)) {
			forward = ListNews(request, response);
		} else if ("getNews".equals(action)) {
			forward = GetNews(request, response);
		}
		request.getRequestDispatcher(forward).forward(request, response);
	}

	/**
	 * 新闻列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	private String ListNews(HttpServletRequest request,
			HttpServletResponse response) {
		String url = null;
		try {
			INews newsImpl = (INews) ImplFactory.getInstance(NewsImpl.class
					.getName());
			List<NewsBean> newss = newsImpl.listNews();
			request.setAttribute("newss", newss);
			url = "../front/news/newslist.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
	}

	/**
	 * 获取新闻
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	private String GetNews(HttpServletRequest request,
			HttpServletResponse response) {
		String url = null;
		int newsid = Integer.parseInt(request.getParameter("newsid"));
		System.out.println("新闻id是:" + newsid);
		try {
			INews newsImpl = (INews) ImplFactory.getInstance(NewsImpl.class
					.getName());
			NewsBean news = newsImpl.getNews(newsid);
			List<NewsBean> newss = newsImpl.listNews();
			request.setAttribute("news", news);
			request.setAttribute("newss", newss);
			url = "../front/news/showNews.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return url;
	}
}

 

index.jsp:

 

只有一个超链接

<a href="../news/listNews.html">查看新闻列表</a>

 

newslist.jsp:

 

<body topmargin="0">
<table id="newslist" cellpadding="7" cellspacing="3" border="0" align="center" width="70%" bordercolor="#FF6600">
	<tr>
    	<td colspan="2"><a href="index.html">首页</a> &gt;&gt; 新闻列表</td>
    </tr>
	<tr>
    	<td class="title" colspan="2">新闻列表</td>
    </tr>
    <c:forEach items="${newss}" var="news">
	<tr>
    	<td width="70%" align="left"><a href="../news/showNews_${news.newsid }.html">${news.title }</a></td>
        <td width="30%" align="right">发布时间:${news.addtime }</td>
    </tr>
    </c:forEach>
</table>
</body>

 

showNews.jsp:

 

<body topmargin="0">
<div id="con">
<table cellpadding="7" cellspacing="3" border="0" align="center" width="100%" bordercolor="#FF6600">
	<tr>
    	<td><a href="index.html">首页</a> &gt;&gt; <a href="listNews.html">新闻列表</a> &gt;&gt; ${news.title }</td>
    </tr>
	<tr>
    	<th class="title">${news.title }</th>
    </tr>
	<tr>
    	<td align="right">来源网址:${news.author } 发表时间:${news.addtime }</td>
    </tr>
    <tr>
    	<td bgcolor="#E8F2FE" class="content">${news.intro }</td>
    </tr>
    <tr>
    	<td class="content">${news.content }</td>
    </tr>
</table>
</div>
<div id="listnew">
	<span style="float: right;height: 30px; width: 100%;position: relative; margin-left: 50px;">新闻列表</span>
	<c:forEach items="${newss }" var="neww">
		<li><a href="../news/showNews_${neww.newsid }.html">${neww.title }</a></li>
	</c:forEach>
</div>
</body>

 

效果图:

 

第一张:这个是列出了全部的新闻标题,如果没有UrlRewrite的话,地址栏实际显示的应该是

http://localhost:8080/fkw/servlet/NewsServlet2?action=listNews

而经过UrlRewrite,目前如下显示

下面每一条新闻标题的超链接都是:http://localhost:8080/fkw/news/showNews_newid.html

newsid为每一条新闻的id

 

 

第二张图:

这张是对某一条新闻的具体显示

实际访问的地址其实是:

http://localhost:8080/fkw/servlet/NewsServlet2?action=getNews&newsid=3

现在如下显示

 

 

   发表时间:2010-01-27  
不好意思,我好像发错板块了
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics