虽然多次基于别人的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
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信息");
}
未完待续。。。