單獨(dú)使用Spring建立簡易開發(fā)框架(四)
Posted on 2007-09-11 19:11 匪客 閱讀(1461) 評論(0) 編輯 收藏 所屬分類: 開發(fā)技術(shù)5. web.xml 配置
5.1. 基本配置
web.xml 采用 servlet2.4 規(guī)范,默認(rèn)配置了會話過期時(shí)間和網(wǎng)站首頁文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
??? <!--
會話的時(shí)間
-->
?
??<session-config>
????????????? <session-timeout>30</session-timeout>
?????? </session-config>
?????? <!--
歡迎頁面
-->
?????? <welcome-file-list>
????????????? <welcome-file>index.jsp</welcome-file>
?????? </welcome-file-list>
??????
</web-app> |
5.2. 加載 spring 監(jiān)聽
在 web.xml 文件中加入如下配置:
<!--
設(shè)置環(huán)境變量,將
web
應(yīng)用根目錄存儲到環(huán)境變量中
-->
<context-param>
??? <param-name>webAppRootKey</param-name>
??? <param-value>webapp.root</param-value>
</context-param>?
<!--
設(shè)置環(huán)境變量指定
applicationContext.xml
配置文件
-->
<context-param>
??? <param-name>contextConfigLocation</param-name>
??? <param-value>
??????? /WEB-INF/applicationContext.xml
??????? /WEB-INF/applicationContext-admin.xml
??????? /WEB-INF/applicationContext-quartz.xml
??? </param-value>
</context-param>???
<!--
設(shè)置
context
的監(jiān)聽,啟動時(shí)加載
applicationContext.xml
文件,初始化
applicationContext
實(shí)例
-->
<listener>
??? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> |
5.3. Log4j 日志
首先,在 web.xml 文件中加入如下配置:
<!--
設(shè)置環(huán)境變量指定
log4j
的配置文件
-->
<context-param>
??? <param-name>log4jConfigLocation</param-name>
??? <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<!--
設(shè)置
log4j
的監(jiān)聽,使日志組件能夠使用,調(diào)用
log4jConfigLocation
中設(shè)置的環(huán)境變量
-->
<listener>
??? <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener> |
log4j.properties 文件放在 /WEB-INF/ 目錄下, 文件內(nèi)容如下例,其中根日志級別為 warn ,而在包 cn.idtag 中的類的日志級別為 info 。
#
定義根日志的級別和輸出路徑
fatal, error, warn, info, debug
log4j.rootCategory=warn, stdout, logfile
#
在包
cn.idtag
中的類的日志級別
log4j.logger.cn.idtag=info
#
日志輸出參數(shù)
#%c:
日志信息所在類名
#%d:
日志信息產(chǎn)生時(shí)間
#%p:
日志信息級別
#%m:
產(chǎn)生的日志具體信息
#%n:
輸出日志信息換行
log4j.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %c%n%m%n%n
#
控制臺輸出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=${log4j.ConversionPattern}
#
日志文件輸出
log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/log.txt
log4j.appender.logfile.Append = true
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=${log4j.ConversionPattern} |
5.4. 解決中文亂碼
在 web.xml 文件中加入如下配置,可以解決表單提交的編碼,不需要在每個(gè) *.jsp 、 *.htm 等頁面上編寫代碼“ <%@ page contentType="text/html; charset=gbk" language="java"%> ”了,并且為瀏覽器端設(shè)置的“ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ”代碼也不需要寫了,瀏覽器會自動識別,注意建立文件時(shí)一定要使用 UTF-8 字符集。
<!--
設(shè)置字符串過濾器,解決表單
Post
提交中的亂碼問題
-->
<filter>
??? <filter-name>characterEncoding</filter-name>
??? <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
??? <init-param>
??
?????<param-name>encoding</param-name>
??????? <param-value>utf-8</param-value>
??? </init-param>
??? <init-param>
??????? <param-name>forceEncoding</param-name>
??????? <param-value>true</param-value>
??? </init-param>
</filter>
<filter-mapping>
??? <filter-name>characterEncoding</filter-name>
??? <url-pattern>/*</url-pattern>
</filter-mapping>
<!--
設(shè)置
jsp,htm,html
頁面的字符集,是否使用標(biāo)簽,是否使用
jsp
腳本等,頭尾載入頁面等
-->
<jsp-config>
??? <jsp-property-group>
??????? <description>
指定
JSP
文件的配置屬性
</description>
??????? <display-name>jspConfiguration</display-name>
??????? <url-pattern>*.jsp</url-pattern>
??????? <el-ignored>false</el-ignored>
??????? <page-encoding>utf-8</page-encoding>
??????? <scripting-invalid>false</scripting-invalid>
??????? <include-prelude></include-prelude>
??????? <include-coda></include-coda>
??? </jsp-property-group>
??? <jsp-property-group>
??????? <description>
指定
htm
文件的配置屬性
</description>
??????? <display-name>jspConfiguration</display-name>
??????? <url-pattern>*.htm</url-pattern>
??????? <el-ignored>false</el-ignored>
??????? <page-encoding>utf-8</page-encoding>
??????? <scripting-invalid>false</scripting-invalid>
??????? <include-prelude></include-prelude>
??????? <include-coda></include-coda>
??? </jsp-property-group>
??? <jsp-property-group>
??????? <description>
指定
html
文件的配置屬性
</description>
??????? <display-name>jspConfiguration</display-name>
??????? <url-pattern>*.html</url-pattern>
??????? <el-ignored>false</el-ignored>
??????? <page-encoding>utf-8</page-encoding>
??????? <scripting-invalid>false</scripting-invalid>
??????? <include-prelude></include-prelude>
??????? <include-coda></include-coda>
??? </jsp-property-group>
</jsp-config> |
如果應(yīng)用服務(wù)器是 Tomcat ,編輯 Tomcat 根目錄下的“ /conf/server.xml ”文件,找到 Connector 配置部分,在配置結(jié)尾加入“ URIEncoding="utf-8" ”:
<Connector port="8080" maxThreads="150" …… disableUploadTimeout="true"/>
改為:
<Connector port="8080" maxThreads="150" …… disableUploadTimeout="true" URIEncoding="UTF-8"/> |
可解決系統(tǒng)內(nèi)部 GET 方法傳遞中文字符參數(shù)的亂碼問題,但是在外部 URL 請求中參數(shù)值帶有中文的,必須使用 URLEncode 編碼。
5.5. 配置 servlet
在 web.xml 文件中配置 servlet 請求路徑和實(shí)現(xiàn)類:
<servlet>
??? <servlet-name>sendSms</servlet-name>
??? <servlet-class>cn.idtag.test.servlet.SendSmsServlet</servlet-class>
</servlet>
<servlet-mapping>
??? <servlet-name>sendSms</servlet-name>
??? <url-pattern>/servlet/sendsms</url-pattern>
</servlet-mapping> |
而 SendSmsServlet.java 文件需要繼承 HttpServlet 類,如果 response 要輸出內(nèi)容并包含非英文字符,要設(shè)置輸出的字符集為 UTF-8 :
package cn.idtag.test.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import java.io.IOException;
public class SendSmsServlet extends HttpServlet
{
??? public void doGet(HttpServletRequest request, HttpServletResponse response)
??????????? throws ServletException, IOException
{
??????? response.setContentType("text/html;charset=utf-8");
??????? PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "utf-8"));
??????? try
??????? {
??????? //do something…
??????? } finally
??????? {
??????????? out.close();
??????? }
??? }
??? public void doPost(HttpServletRequest request, HttpServletResponse response)
??????????? throws ServletException, IOException
??
?{
??????? doGet(request, response);
??? }
} |