浏览 2304 次
锁定老帖子 主题:过滤器 解决中文乱码
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-09-23
[/align][/size]
本文主要讲解在网站开发中两个方面的问题,一是中文乱码,二是 css 的控制:比如标题栏按钮按下的颜色控制等,例如优酷的标题栏,处在哪个标题下该标题颜色就会加深。涉及到了jsp的动作标签<jsp:include page=""><jsp:param name="" value="" /> [color=red]注意:所涉及的css只要在web-inf下建个css文件夹在建一个空css文件把我给的代码拷进去就可以了[/color] [[size=x-large][/color][align=center] 1.设计较为简单,主要涉及两个jsp页面。其中的一个包含另一个并向其传递参数(中文参数)为了解决中文乱码问题而使用filter.当然也可以不传递中文,主要是为了学习一下fiter的使用并检验一下。 2. [color=orange]第一个jsp页面为head.jsp代码如下 <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!-- Header --> <div id="header"> <h1 id="logo"> <a href="./" title="[Go to homepage]"><img src="tmp/logo.gif" alt="" /></a> </h1> <hr class="noscreen" /> <!-- Navigation --> <div id="nav"> <a href="#" id="nav-active">Homepage</a><span>|</span> <a href="#">About us</a><span>|</span> <a href="#">Support</a><span>|</span> <a href="#">Contact</a> </div> <!-- /nav --> </div> <!-- /header --> <!-- Tray --> <div id="tray"> <ul> <c:if test="${param.page eq '主页'}"> <li id="tray-active"><a href="#">个人中心</a></li> </c:if> <c:if test="${param.page ne '主页''}"> <li><a href="#">个人中心</a></li> </c:if> <c:if test="${param.page eq '第二页'}"> <li id="tray-active"><a href="#">个人信息维护</a></li> </c:if> <c:if test="${param.page ne '第二页'}"> <li><a href="#">个人信息维护</a></li> </c:if> <c:if test="${param.page eq 'jobinfo'}"> <li id="tray-active"><a href="#">就业信息维护</a></li> </c:if> <c:if test="${param.page ne 'jobinfo'}"> <li><a href="#">就业信息维护</a></li> </c:if> </ul> <!-- Search --> <div id="search" class="box"> <form action="#" method="get"> <div class="box"> <div id="search-input"> <span class="noscreen">Search:</span> <input type="text" size="30" name="" value="Search" /> </div> <div id="search-submit"> <input type="image" src="design/search-submit.gif" value="OK" /> </div> </div> </form> </div> <!-- /search --> <hr class="noscreen" /> </div> <!-- /tray --> 3.第二个jsp test.jsp 代码如下 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="content-language" content="en" /> <meta name="robots" content="all,follow" /> <meta name="author" lang="en" content="All: Your name [www.url.com]; e-mail: info@url.com" /> <meta name="copyright" lang="en" content="Webdesign: Nuvio [www.nuvio.cz]; e-mail: ahoj@nuvio.cz" /> <meta name="description" content="..." /> <meta name="keywords" content="..." /> <link rel="stylesheet" media="screen,projection" type="text/css" href="css/reset.css" /> <link rel="stylesheet" media="screen,projection" type="text/css" href="css/main.css" /> <!--[if lte IE 6]><link rel="stylesheet" type="text/css" href="css/main-msie.css" /><![endif]--> <link rel="stylesheet" media="screen,projection" type="text/css" href="css/style.css" /> <link rel="stylesheet" media="print" type="text/css" href="css/print.css" /> <title>Impress</title> </head> <body> <div id="main"> <!-- Header --> <jsp:include page="include/stdHead.jsp"> <jsp:param name="page" value="主页"/> //此处的value值自己变动看效果 </jsp:include> </div> </body> </html> 3.EncodingFiter.java的代码如下 package com.czg.common; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class EncodingFilter implements Filter { protected FilterConfig filterConfig; protected String targetEncoding = null; public void destroy() { this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("使用"+ targetEncoding + "编码"); HttpServletRequest request2 = (HttpServletRequest)request; request2.setCharacterEncoding(targetEncoding); chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.targetEncoding = filterConfig.getInitParameter("enconding"); } public void setFilterConfig(FilterConfig filterConfig) { this.filterConfig = filterConfig; } } 4.配置xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>encoding</filter-name> <filter-class>com.czg.common.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 5.css的代码如下 代码压缩文件上传不上去,有需要和我联系villain564@sina.com 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-09-23
http://www.abab123.com/bbs/down.asp?html=1042745
|
|
返回顶楼 | |