s2sh框架搭建(辅助工具:MyEclipse)及解决一些遇到的问题

1.新建一个web project

2.首先生成Hibernate Facet

3.Hibernate Facet 安装步骤

 

4.然后是spring facet安装步骤

 

5.最后是struts facet 的配置

 6.最后的整体布局如下所示

7.在服务器上运行,发现如下错误:

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)........

主要是没有在applicationContext.xml中配置 DataSource 这个bean,配置好后将其注入sessionFactory

<!--新加入 datasource --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/hjzgg_test?characterEncoding=UTF-8" /><property name="username" value="root" /><property name="password" value="hjzgg5211314" /></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="configLocation"value="classpath:hibernate.cfg.xml"></property><!--新加入 将dataSource注入到sessionFactory中 --><property name="dataSource" ref="dataSource"></property><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.MySQL5Dialecthibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=falsehibernate.cache.use_second_level_cache=truehibernate.cache.use_query_cache=falsehibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.current_session_context_class=thread <!-- 解决Hibernate4 No Session found for current thread -->
</value> </property>

8.新建PersonDao(DAO层),它的实现类PersonDaoImpl,以及 Action类 LoginAction

在applicationContext.xml中添加如下代码:

    <bean id="personDao" class="com.xunchang.PersonDaoImpl"><!-- 采用依赖注入传入SessionFactory的引用 --><property name="sessionFactory" ref="sessionFactory"/></bean><bean id="login" class="com.xunchang.LoginAction">       <property name="personDao" ref="personDao"/></bean>

9.POJO 与 hibernate层:新建POJO(普通java类, 对于每一个变量拥有getter 和 setter方法),新建 映射hibernate持久化类person.cfg.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.xunchang"><class name="Person" table="home402"><!-- 定义持久化类的表示属性 --><id name="personId" column="personId" type="java.lang.Integer"><!-- 定义主键生成策略 --><generator class="identity"/></id><property name="person_name" column="person_name" type="java.lang.String"/><property name="person_age" column="person_age" type="java.lang.Integer"/></class>
</hibernate-mapping>

然后在applicationContext.xml中的sessionFactory(bean)中加载该映射文件如下:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="configLocation"value="classpath:hibernate.cfg.xml"></property><!--新加入 将dataSource注入到sessionFactory中 -->.............<!--通过配置文件的方式获取数据源--><property name="mappingResources"><list><!-- 以下用来列出所有的PO映射文件 --><value>person.cfg.xml</value></list></property>       </bean>

10.运行服务器,问题又来了,如下:

org.hibernate.HibernateException: No Session found for current threadorg.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041)com.xunchang.PersonDaoImpl.getSession(PersonDaoImpl.java:16)com.xunchang.PersonDaoImpl.findAllPerson(PersonDaoImpl.java:63)com.xunchang.LoginAction.execute(LoginAction.java:20)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Hibernate4 No Session found for current thread原因:  http://www.yihaomen.com/article/java/466.htm

解决方法:在applicationContext.xml中的sessionFactory 中<property name="hibernateProperties"></property>加入 hibernate.current_session_context_class=thread

 <property name="hibernateProperties"><value>......hibernate.current_session_context_class=thread <!-- 解决Hibernate4 No Session found for current thread --></value> </property>

11.最后一个问题就是hibernate 中文乱码,解决方案如下:

//写一个字符集过滤器
import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet;
public class SetCharacterEncodingFilter extends HttpServlet implements Filter {public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); chain.doFilter(request, response); } public void init(FilterConfig config) throws ServletException{ } public void destroy(){ } }

//在web.xml中加入如下代码,问题解决

<filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>com.xunchang.SetCharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

 

 

转载于:https://www.cnblogs.com/hujunzheng/p/4366980.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/531513.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

520爱心表白——C语言入门

520爱心表白——C语言入门 关于爱心表白的代码&#xff0c;网上有很多非常好看而且可以实现颜色变换和立体&#xff0c;动态等效果的代码。但是我入门不久&#xff0c;能力有限。520重要的可能还是在心意我觉得&#xff0c;所以自己写了一个非常简单毫无技术含量爱心代码来表达…

c语言之结构

今天来说一下C语言里的结构体(struct)、共用体(l联合体)union、枚举。 欢迎加入嵌入式学习群&#xff1a;559601187 &#xff08;一&#xff09;结构体&#xff1a;struct 1.1 概念 是一种自定义的数据类型结构体是构造类型的一种不同数据类型的集合地址空间连续&#xff0c;…

C语言实现音乐播放器(Linux madplay)

&#xff08;一&#xff09;需求分析 1.扫描指定路径下的音乐&#xff0c;并显示出来 2.实现音乐的播放、暂停、上一首和下一首的功能 3.程序退出释放内存资源 &#xff08;二&#xff09;思路 1.扫描出指定路径下的音乐文件(便利指定文件夹&#xff0c;找出音频文件放在数组…

虚拟机中安装linux

&#xff08;一&#xff09;前言 就在昨天电脑的固态突然崩掉&#xff0c;无奈重新把系统装在的以前的硬盘上&#xff0c;为了能够继续工作重新配置嵌入式linux系统开发环境&#xff0c;本教程主要记录在虚拟机中安装linux。 &#xff08;二&#xff09;环境准备 虚拟机&…

Ubuntu设置root登录

1.、Ubuntu 管理员用户 root 默认没有密码&#xff0c;在使用前最好添加密码&#xff0c;使用指令&#xff1a; sudo passwd root 注意&#xff1a;命令行输入密码时不显示&#xff0c;输入时需注意密码的准确性&#xff1b; 2、Ubuntu 想要用 root 帐户登录&#xff0c;可在普…

vim配置之spacevim

为了更好的利用vim&#xff0c;我们一般需要自己配置&#xff0c;今天介绍了一下经常用的spacevim &#xff08;一&#xff09;配置环境 Ubuntu16.04vim 7.4版本以上(必须&#xff01;&#xff01;) &#xff08;二&#xff09;安装spacevim 1.检查vim的版本&#xff1a; v…

Ubuntu更换gnome桌面环境后不能root登录

安装完Ubuntu后感觉界面有点丑陋&#xff0c;安装了gnome桌面环境试一下 sudo apt-get install gnome-shell sudo apt-get install ubuntu-gnome-desktop如果选择了lightdm后可以使用sudo dpkg-reconfigure gdm3 重新改回gdm3 sudo apt-get install unity-tweak-tool sudo ap…

Ubuntu下安装tilix终端仿真器

安装环境 Ubuntu 16.04 操作步骤 首先添加这个终端模拟器仓库的公钥。这里我都是以root超级用户权限操作的&#xff0c;如果没有的话&#xff0c;请在命令前面加sudo。 add-apt-repository ppa:webupd8team/terminixapt update安装Tilix。 apt install tilix安装完成测试结…

vim配置之snippets代码块

&#xff08;一&#xff09;目的 我们在编写程序的过程中&#xff0c;经常会敲一些重复的代码&#xff0c;我们可以利用snippets来达到输入简写来敲出完整的代码 &#xff08;二&#xff09;安装步骤 安装使用Vundle,没有vbundle的先执行下面的命令 git clone https://gith…

c语言实现跳动的心

本文章分为两部分&#xff1a;第一部分为实现多彩的心&#xff0c;第二部分是实现心得跳动&#xff0c;两个代码均独立运行 本篇文章转载自公众号&#xff1a; C语言程序设计基础知识 &#xff08;一&#xff09;C语言实现多彩的心 实现过程其实很简单 首先使用for循环绘制心…

Linux下使用消息队实现 ATM 自动取款机功能

文章分五部分&#xff1a;需求分析、项目所需知识点、思路讲解、代码实现、功能演示 本文内容较长&#xff0c;建议是按照我自己的思路给大家讲解的&#xff0c;如果有其他问题&#xff0c;欢迎评论区讨论 文章中的代码是在linux下编译实现的&#xff0c;注意自己的环境。 &…

200行代码实现视频人物实时去除

今天在GitHub上发现了一个好玩的代码&#xff0c;短短几百行代码就实现了从复杂的背景视频中去除人物&#xff0c;不得不说这位大佬比较厉害。 这个项目只需要在网络浏览器中使用JavaScript&#xff0c;用200多行TensorFlow.js代码&#xff0c;就可以实时让视频画面中的人物对…

codeforces C. Vanya and Scales

C. Vanya and ScalesVanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 grams where w is some integer not less than 2(exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using …

菱形继承和虚继承、对象模型和虚基表

1.菱形继承&#xff08;钻石继承&#xff09;&#xff1a;两个子类继承同一父类&#xff0c;而又有子类同时继承这两个子类。例如B,C两个类同时继承A&#xff0c;但是又有一个D类同时继承B,C类。 2.菱形继承的对象模型 class A { public:int _a; };class B :public A { p…

c++虚函数和虚函数表

前言 &#xff08;1&#xff09;虚基表与虚函数表是两个完全不同的概念 虚基表用来解决继承的二义性(虚基类可以解决)。虚函数用来实现泛型编程&#xff0c;运行时多态。 &#xff08;2&#xff09;虚函数是在基类普通函数前加virtual关键字&#xff0c;是实现多态的基础 &a…

VS2017安装配置Qt

这篇文章作为qt的开发环境配置篇&#xff0c;记录如何在vs2017中安装qt的 所需软件下载链接如下&#xff1a; QT下载链接&#xff1a;QT visual studio下载链接&#xff1a;visual studio 这里推荐安装最新的&#xff0c;原因是vs2017不支持一些老版本的makefile文件生成&#…

STM32位带区和位带别名区的浅谈

1.首先谈下为什么要使用位带&#xff1f; 在学习51单片机时就已经使用过位操作&#xff0c;比如使用sbit对单片机IO口的定义&#xff0c;但是STM32中并没有这类关键字&#xff0c;而是通过访问位带别名区来实现&#xff0c;即通过将每个比特位膨胀成一个32位字&#xff0c;当访…

Hibernate的数据查找,添加!

1.首先看一下测试数据库的物理模型 2.测试所需要的Hibernate的jar包 3.数据库的sql /**/ /* DBMS name: MySQL 5.0 */ /* Created on: 2015/7/3 23:17:57 */ /**/drop table if exists books;drop tab…

新手如何在Altium Designer中绘制电路板

好久没用AD画电路板了&#xff0c;这次电子实训让画个PCB板&#xff0c;借着这个机会写了一篇新手教程。 此教程所用的电路图是自动循迹小车&#xff0c;虽然元件比较简单&#xff0c;但是感觉还是很厉害的&#xff0c;一块看一下吧。 此教程仅适用于没有基础的同学 一、概述 …

Qt模仿QQ登录界面(一)

这两天研究qt&#xff0c;练习时做了个仿QQ登录界面&#xff0c;我这次实现的比较简单&#xff0c;先在这里记录一下&#xff0c;以后有空了会继续完善的。 &#xff08;一&#xff09;效果图 这里使用我的qq号测试的如图&#xff1a; &#xff08;二&#xff09;工程文件 &…