畢設系列—服務端:Java-SSH框架(一)

雖然多次基於別人的SSH框架開發過一些功能,但還是覺得自己重新夯實一下基礎比較好,於是打算重頭試一試SSH框架。接下來,我就開發中遇到的一些問題做一下筆記:

基本的開發環境
異常:Context initialization failed
.action與.*的過濾,jsp被過濾
SSH框架中配置 log4j 的方法

基本的開發環境

  • Jdk 1.7.0_79
  • MyEclipse 10
  • Tomcat 7.x
  • Mysql Workbench 6.2 CE
  • Android Studio 2.0
步驟一
jdk+myeclipse+tomcat,安裝好
新建一個Web Project,配置 web.xml,成功運行
步驟二
右鍵項目,選擇MyEclipse->Project Facts->Struts 2.1
右鍵項目,選擇MyEclipse->Project Facts->Spring 3.1.1
右鍵項目,選擇MyEclipse->Project Facts->Hibernate 3.3.2
連接數據庫測試,記得導入mysql-connector-java-5.1.7-bin.jar
PS:需要注意的是安裝Hibernate時, 最好不要勾選Create SessionFactory class。
步驟三
導入JSTL1.2.1Library,右擊項目->Build Path->Add Library->MyEclipse Library->Next->JSTL1.2.1Library->Finish

ibernate配置H

異常:Context initialization failed

在整合S2SH架構,配置Spring3 的事務管理的時候,添加一些tx-advise以及aop等等事務配置,在運行時發現以下錯誤。
異常

Context initialization failed
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element ‘context:annotation-config’.

主要原因

是xml開頭沒寫好,把aop,tx等的schemalocation配置好就行了

解決方案

將xml開頭補全,如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop     
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context     
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

.action與.*的過濾,jsp被過濾

導入struts2.1,我默認的是是進行.action的過濾,然後發現,在index.jsp頁面,弄一個登錄,但在服務器運行後,點擊index.jsp頁面的鏈接,報錯
報錯:The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. 異常

主要原因:web.xml配置

<filter>
      <filter-name>struts2</filter-name>
      <filter-class>
          org.apache.struts2.dispatcher.ng.filter.StrutsPrepar                  eAndExecuteFilter
      </filter-class>
  </filter> 
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>*.action</url-pattern>
  </filter-mapping>

解決方案

1.將web.xml下的struts2過濾器的過濾方式從.action改爲/*; 不過這種方式自己感覺不太好,應該這樣默認就將所有的進行了過濾,可能會對有些應用帶來麻煩,例如editor的配置
2. 修改jsp 文件,不使用 struts 的標籤
3.就是在web.xml配置文件中再添加一個filter:

<filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>*.jsp</url-pattern>
  </filter-mapping>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>*.action</url-pattern>
  </filter-mapping>

SSH框架中配置 log4j 的方法

步驟一
導入log4j-1.2.17.jar以及commons-logging-1.2.jar
步驟二
新建log4j.properties或log4j.xml文件
# Set root logger level to error
log4j.rootLogger=INFO, Console, File
###### Console appender definition #######
# All outputs currently set to be a ConsoleAppender.
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c{3}] %m%n
#log4j.appender.Console.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c] %m%n

###### File appender definition #######
log4j.appender.File=org.apache.log4j.DailyRollingFileAppender
log4j.appender.File.File=spring.log
log4j.appender.File.Append=false
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c] %m%n
步驟三
代碼中導入及使用
然後在代碼裏可以類
private static final Log logger = 
      LogFactory.getLog(XmlHelper.class);

: 需要打log的地方

if(logger.isDebugEnabled()){
    logger.debug("debug信息");
}

未完待續。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章