我有一个正常工作的春天(4.2.4)&我实现了Tiles(3.0.5)的Hibernate / MySql项目。现在,当我运行我的项目并按照链接到我的" herp" view(正式显示数据库查询返回的记录列表)调试显示我的Autowired SessionContext对象为null;后者在我的根上下文中定义为bean。 static 页面的Tiles / JSP正在按预期工作,我的控制器正在“触发”。我是所有这些框架和后续教程的新手,所以希望是一个愚蠢的配置错误......?提前谢谢。
的web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>herpWeb</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/spring/log4j.xml</param-value>
</context-param>
<filter>
<filter-name>encoding-filter</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>
</filter>
<!-- Processes application requests -->
<servlet>
<servlet-name>tiles</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/servlet-context.xml</param-value> <!-- Anything related to web tier -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>tiles</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
根context.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<tx:annotation-driven />
<context:component-scan base-package="uk.ac.open.my.dja389.herp" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/herp" />
<property name="username" value="j2ee" />
<property name="password" value="j2ee" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="uk.ac.open.my.dja389.herp.**.*" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
servlet的context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="uk.ac.open.my.dja389.herp.controller" />
<!-- Used to serve static resources like css, images and javascript files -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/pages directory -->
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-defs/templates.xml</value>
</list>
</property>
</bean>
HerpController.java
package uk.ac.open.my.dja389.herp.controller;
@Controller
public class HerpController {
private HerpBean herpBean;
@Autowired
private HerpService herpService;
@Autowired
private HerpRepository herpRepository;
public HerpController() {
this.herpBean = new HerpBean();
}
@RequestMapping(value = "/herp", method = RequestMethod.GET)
public ModelAndView get() {
ModelAndView model = new ModelAndView("herp");
model.addObject(this.herpBean);
model.addObject("herpRepo", this.herpRepository);
return model;
}
}
HerpRepositoryImpl.java
package uk.ac.open.my.dja389.herp.repositories;
@Repository("herpRepository")
public class HerpRepositoryImpl implements HerpRepository {
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public void save(Herp herp) {
Session currentSession = this.sessionFactory.getCurrentSession();
currentSession.saveOrUpdate(herp);
}
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public List<Herp> getAllHerps() {
<<<debug here reports sessionFactory is null>>>
List<Herp> allHerps = this.sessionFactory.getCurrentSession().createQuery("from Herp").list();
return allHerps;
}
}
例外(摘录)
Request processing failed; nested exception is
org.apache.tiles.request.render.CannotRenderException: JSPException including path '/WEB-INF/pages/herp.jsp'.
java.lang.NullPointerException
uk.ac.open.my.dja389.herp.repositories.HerpRepositoryImpl.getAllHerps(HerpRepositoryImpl.java:29)