记录一下Maven整合spring,hibernate,strusts2我程序中出的bug

action类如下

package com.itheima.movenweb.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.junit.Test; import com.itheima.movenweb.domain.Dep;
import com.itheima.movenweb.service.Service;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class Action extends ActionSupport { private Service service; public Service getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
public String findDepList(){
System.out.println(1);
List<Dep> list = service.dolist();
System.out.println(service);
ServletActionContext.getRequest().setAttribute("list", list);
return "success";
} }

serviceImpl如下:

package com.itheima.movenweb.serviceImpl;

import java.util.List;

import org.junit.Test;

import com.itheima.movenweb.dao.Dao;
import com.itheima.movenweb.domain.Dep;
import com.itheima.movenweb.service.Service; public class ServiceImpl implements Service { private Dao dao; public Dao getDao() {
return dao;
} public void setDao(Dao dao) {
this.dao = dao;
} @Override
public List<Dep> dolist() {
System.out.println(2);
List<Dep> list = dao.dolist();
return list;
} }

daoImpl如下:

package com.itheima.movenweb.daoImpl;

import java.util.List;

import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.itheima.movenweb.dao.Dao;
import com.itheima.movenweb.domain.Dep; public class DaoImpl extends HibernateDaoSupport implements Dao{ @Override
public List<Dep> dolist() {
System.out.println(3);
List<Dep> list = (List<Dep>) this.getHibernateTemplate().find("from Dep");
return list;
} }

applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
"> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 在这里方法上开启事务,不要搞错了 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="list" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* com.itheima.movenweb.serviceImpl.*.*(..))"/>
<aop:advisor pointcut-ref="serviceMethod" advice-ref="advice" />
</aop:config> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!--
<property name="url" value="jdbc:mysql://127.0.0.1:3306/movenwebtest?useUnicode=true&characterEncoding=UTF8"/>
-->
<property name="url" value="jdbc:mysql:///movenwebtest11?useUnicode=true&characterEncoding=UTF8"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
<property name="mappingLocations">
<value>classpath:com/itheima/movenweb/domain/*.hbm.xml</value>
</property>
</bean> <!-- 部门数据访问类 -->
<bean id="depDao" class="com.itheima.movenweb.daoImpl.DaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- 部门业务逻辑类 -->
<bean id="depService" class="com.itheima.movenweb.serviceImpl.ServiceImpl">
<property name="dao" ref="depDao"></property>
</bean> <!-- 部门action -->
<bean id="depAction" class="com.itheima.movenweb.action.Action">
<property name="service" ref="depService"></property>
</bean> </beans>

  struts.xml 如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="index" class="depAction" method="findDepList">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

还有hibernate的配置文件,我报错的原因和他没有关系,我就不放在这里了;以上的代码是修改后正确的代码;

下面我放入我错误的代码,第一个错误在struts2的配置文件,我将class的配置信息配成了类的全路径,然后报空指针异常,这里class的路径应该写已经配置在applicationContext.xml中的action的id的值

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="index" class="com.itheima.movenweb.domain.Dep" method="findDepList">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

  然后又出了一个问题:如下

记录一下Maven整合spring,hibernate,strusts2我程序中出的bug

我的action中的输出语句输出了,但是我的数据库没有连接上,我去查询我的数据库,发现我的数据库名字是“mavenwebtest11;”  对,你没有看错,我的数据库名字多加了“;”我发现我在创建数据库后本来是想加上英文状态的结束符号,结果加成了中文的结束符号,然后就被一起写进了数据库的名字中,大写的尴尬呀,作为我的第一篇博客,谨以此来激励自己不断学习,不断成长

上一篇:用 Python 写 Robot Framework 测试


下一篇:MongoDB 用MongoTemplate查询指定时间范围的数据