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

three-god 2018-05-02 原文

记录一下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>

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

 

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

发表于 2018-05-02 22:51 神之左手创世 阅读() 评论() 编辑 收藏

 

版权声明:本文为three-god原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/three-god/p/8983010.html

记录一下Maven整合spring,hibernate,strusts2我程序中出的bug的更多相关文章

  1. 如何将JAR包发布到Maven中央仓库?

    将jar包发布到Maven中央仓库(Maven Central Repository),这样所有的Java开发 […]...

  2. maven-publish 使用发布 andorid aar 到本地仓,项目仓或module 仓

    apply plugin: 'maven-publish'println ""/*切勿定义:groupId,artifactId,versio...

  3. myeclipse maven 安装 myeclipse 上安装 Maven3

    环境准备: JDK 1.6.45 Maven 3.0.5 myeclipse 8.5 安装 Maven 之前要 […]...

  4. 如何向Maven仓库(私服)中上传第三方jar包

      本文详细介绍如何向maven仓库中上传第三方jar包。   1、在本地maven安装路径中找到conf文件 […]...

  5. Java 在PPT中创建SmartArt图形、读取SmartArt图形中的文本

    一、概述及环境准备 SmartArt 图形通过将文字、图形从多种不同布局、组合来表现内容和观点的逻辑关系,能够 […]...

  6. Maven打包跳过测试

    使用Maven打包的时候,可能会因为单元测试打包失败,这时候就需要跳过单元测试。 Maven跳过单元测试有三种 […]...

  7. Maven打包时出现“Show Console View”错误弹出框,错误详情为“An internal error has occurred. java.lang.NullPointerException”的解决方法

    今天为项目打包时出现了下面的错误提示: 打开Details里面写的是“An internal error ha […]...

  8. IDEA设置maven修改settings.xml配置文件无法加载仓库

    作为初学者配置maven一般网上搜索。然后你就看到各种配置文件片段,首先配置镜像,然后配置仓库。完事后再IDE […]...

随机推荐

  1. “好的” 关系数据库系统应具有的特点

    “好的” 关系数据库系统应具有的特点: 适度减少数据冗余 对关系模式的属性间允许的数据依赖加以限制,减少表中非 […]...

  2. 银行核心项目之测试阶段 – zouhui

    银行核心项目之测试阶段 2019-05-15 20:34  zouhui  阅读(8412)  评论(0)  […]...

  3. 钽电容封装大全及技术参数

    钽电容封装大全及技术参数 长的话是+-0.2 ,宽是+-0.1 高 (MM)A 型的尺寸3.2 X1.6 X1 […]...

  4. Web自动化selenium技术快速实现爬虫

    selenium是大家众所周知的web自动化测试框架,主要用来完成web网站项目的自动化测试,但其实如果要实现 […]...

  5. 如何隐藏硬盘分区,文件夹

    重要资料,放在硬盘的某个分区上,为了防止别人偷窥,可以任意隐藏某个或几个分区。具体方法如下:       方法 […]...

  6. MySQL 视图(View)

    为了更加合法合规运营网站,我们正在对全站内容进行审核,之前的内容审核通过后才能访问。 由于审核工作量巨大,完成 […]...

  7. Linux入门基础教程

    转载自:http://www.centoscn.com/CentOS/2015/0528/5555.html […]...

  8. 高级搜索指令与百度搜索实践

    引用 https://www.jianshu.com/p/548fe1cb496c...

展开目录

目录导航