spring+hibernate+Struts2 整合(全注解及注意事项)

时间:2022-06-10 04:07:55
 最近帮同学做毕设,一个物流管理系统,一个点餐系统,用注解开发起来还是很快的,就是刚开始搭环境费了点事,今天把物流管理系统的一部分跟环境都贴出来,有什么不足的,请大神不吝赐教。
  
  1、结构如下
    

  spring+hibernate+Struts2 整合(全注解及注意事项)

  2、jar包如下

  spring+hibernate+Struts2 整合(全注解及注意事项)

  3、首先是spring.xml
  
spring+hibernate+Struts2 整合(全注解及注意事项)
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="cn.lGo" /> <context:property-placeholder location="WEB-INF/jdbc.properties" /> <bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <bean id="sf"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>cn.lGo.entity</value> </list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sf"></property>
</bean> <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf" />
</bean> <!-- 配置事务特性 -->
<tx:advice id="idAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" propagation="NOT_SUPPORTED" read-only="true" isolation="READ_COMMITTED" />
<tx:method name="save*" propagation="REQUIRED" isolation="READ_COMMITTED" />
<tx:method name="delete*" propagation="REQUIRED" isolation="READ_COMMITTED" />
<tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED" />
<!-- Action中execute,暂时先用REQUIRED,以后真对查询操作, 修改以后在使用NOT_SUPPORTED -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice> <!-- Aop配置事务作用在那个位置:dao-->
<aop:config>
<aop:pointcut expression="execution (* cn.lGo.dao..*.*(..))" id="curd"/>
<aop:advisor advice-ref="idAdvice" pointcut-ref="curd"/>
</aop:config>
</beans>
spring+hibernate+Struts2 整合(全注解及注意事项)
  这个配置文件就不说了,在之前的文章中有提到过的。

4、web.xml
spring+hibernate+Struts2 整合(全注解及注意事项)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>login.ftl</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
spring+hibernate+Struts2 整合(全注解及注意事项)
上述中:
  
spring+hibernate+Struts2 整合(全注解及注意事项)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
spring+hibernate+Struts2 整合(全注解及注意事项)
指的是在项目启动时加载spring的配置文件
spring+hibernate+Struts2 整合(全注解及注意事项)
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
spring+hibernate+Struts2 整合(全注解及注意事项)
指的是配置使用struts2注解

5、一个实体类
spring+hibernate+Struts2 整合(全注解及注意事项)
package cn.lGo.entity;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="user")
public class User { private Integer id;
private String name;
private String password;
private String address;
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
} }
spring+hibernate+Struts2 整合(全注解及注意事项)
其中要注意的问题是,如果数据库中有字段为user_name,而在实体类中写userName,是不会自动映射的,要加一个注解@Column(name="user_name")。
另外,还有一点实体类中使用hibernate注解时,不要使用数据库的敏感词 6、dao
spring+hibernate+Struts2 整合(全注解及注意事项)
package cn.lGo.dao;

import java.util.List;

import cn.lGo.entity.User;

public interface UserDao {

    /**
* 根据用户名查找对应用户
* @param userName
* @return
*/
public List<User> findUserByName(String userName);
}
spring+hibernate+Struts2 整合(全注解及注意事项)
7、daoImpl 实现类
spring+hibernate+Struts2 整合(全注解及注意事项)
package cn.lGo.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component; import cn.lGo.dao.UserDao;
import cn.lGo.entity.User;
@Component
public class UserDaoImpl implements UserDao {
private HibernateTemplate hibernateTemplate; public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
} @SuppressWarnings("unchecked")
public List<User> findUserByName(String userName) {
return hibernateTemplate.find("from User where name= ? ",new Object[]{userName});
} }
spring+hibernate+Struts2 整合(全注解及注意事项)
这个在spring注解呢篇中说过。
@Component 是将该类注册到spring中,如果没写属性,就会相当于 <bean name="userDaoImpl" ... 首字母小写
@Resource 注入,将sprin容器中name="hibernateTemplate"的 注入进去 8、service
spring+hibernate+Struts2 整合(全注解及注意事项)
package cn.lGo.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.lGo.dao.UserDao;
import cn.lGo.entity.User; @Component
public class UserService { private UserDao userDao; public UserDao getUserDao() {
return userDao;
} @Resource(name="userDaoImpl")
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public List<User> findUserByName(String userName){
return userDao.findUserByName(userName);
}
}
spring+hibernate+Struts2 整合(全注解及注意事项)
@Component还是把name="userService"的bean注册到spring容器
这个@Resource是将name="userDaoImpl"注给userDao,面向接口的编程,更具有灵活性 9、Action
spring+hibernate+Struts2 整合(全注解及注意事项)
package cn.lGo.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Component; import com.opensymphony.xwork2.ActionContext; import cn.lGo.entity.User;
import cn.lGo.service.UserService; @Component("loginAction")
@Namespace(value="/checkUser")
@ParentPackage("json-default")
@Results({
@Result(name="success",type="json")
})
public class LoginAction { private String userName;
private String password;
private String flag;
private UserService userService; public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
@Resource
public void setUserService(UserService userService) {
this.userService = userService;
} public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
System.out.println("LoginAction.execute()");
List<User> listUser=userService.findUserByName(userName);
if(listUser.size()==0){
flag="0";
}else{
if(listUser.get(0).getPassword().equals(password)){
flag="2";
User user=new User();
user.setName(userName);
user.setPassword(password);
user.setAddress(listUser.get(0).getAddress());
ActionContext.getContext().getSession().put("user", user);
}else{
flag="1";
}
}
return "success";
} }
spring+hibernate+Struts2 整合(全注解及注意事项)
@Component("loginAction")将action注册到容器,并不是直接通过struts2访问的,他会根据@Namespace(value="/checkUser"),找到action的type(地址),通过spring容器反射出一个这样的action。
另外还需要很注意的一点!!!!! 在action中写的注入是不能有get()方法的,只能由set(),就像文中userService一样,具体不清楚,还望大婶赐教。。。。 10、页面 login.ftl
spring+hibernate+Struts2 整合(全注解及注意事项)
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="dist/js/jquery-1.7.1.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="dist/js/bootstrap.min.js"></script>
<!-- Bootstrap -->
<link rel="stylesheet" href="dist/css/bootstrap.min.css">
<link rel="stylesheet" href="dist/css/bootstrap-theme.css"> <style>
body{
text-align: center;
} </style>
<script type="text/javascript">
$(function(){
$("#sign").click(function(){
var name=$("#userName").val();
var password=$("#password").val();
if(name==""||password==""){
alert("用户名或密码不能为空");
$("#userName").val("");
$("#password").val("");
return;
}
if(name=="admin"&&password=="admin"){
location.href="admin/main.ftl";
return;
}
$.ajax({
"url":"checkUser/login",
"type":"post",
"data":{
"userName":name,
"password":password
},
"dataType":"json",
"success":function(data){
if(data.flag=="0"){
$("#userName").val("");
$("#password").val("");
alert("用户名不存在,请重新输入");
return;
}else if(data.flag=="1"){
$("#userName").val("");
$("#password").val("");
alert("密码不正确,请重新输入");
return;
}else if(data.flag=="2"){
location.href="customer/userMain.ftl";
}
}
});
});
});
</script>
</head>
<body > <div class="container" id="div1">
<p class="navbar-text navbar-left" style="font-weight:bold;">欢迎使用1Go物流管理系统</p>
<div class="jumbotron" >
<form class="form-inline" role="form" >
<div class="form-group">
<input type="text" class="form-control" id="userName" placeholder="UserName">
</div>
<div class="form-group">
<input type="password" class="form-control" id="password" placeholder="Password">
</div>
<button type="button" class="btn btn-default" id="sign">Sign in</button>
</form>
</div> </div> </body>
</html>
spring+hibernate+Struts2 整合(全注解及注意事项)
前台页面用的是BootStrap3来做的,感觉有种高大上的感觉,hiahiahiahia

这是该项目中的登录部分,麻雀虽小五脏俱全,渐渐的感觉生活少了些刺激感,嫩们怎么寻找生活的刺激。。。

spring+hibernate+Struts2 整合(全注解及注意事项)的更多相关文章

  1. Spring&plus;Hibernate&plus;Struts2整合之实现登录功能

    前端代码: <form id="loginForm" action="${ pageContext.request.contextPath }/user_login ...

  2. 基于IDEA 最新Spirng3&period;2&plus;hibernate4&plus;struts2&period;3 全注解配置 登录

    原文 基于IDEA 最新Spirng3.2+hibernate4+struts2.3 全注解配置 登录 首先说说 IDEA 12,由于myeclipse越来越卡,我改用idea12 了,发现其功能强悍 ...

  3. Spring与Struts2整合VS Spring与Spring MVC整合

    Spring与Struts2整合,struts.xml在src目录下 1.在web.xml配置监听器 web.xml <!-- 配置Spring的用于初始化ApplicationContext的 ...

  4. Spring与Shiro整合 静态注解授权

    Spring与Shiro整合 静态注解授权 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 使用Shiro的种类 首先,Shiro的授权方式共有三种: 1.编程式授权(不推荐) 2. ...

  5. spring与struts2整合出现错误HTTP Status 500 - Unable to instantiate Action

    在进行spring和struts2整合的时候因为大意遇到了一个问题,费了半天神终于找到了问题所在,故分享出来望广大博友引以为戒!! 我们都知道在spring和struts2整合时,spring接管了a ...

  6. SSH&lpar;Struts2&plus;Spring&plus;Hibernate&rpar;框架搭建流程&lt&semi;注解的方式创建Bean&gt&semi;

    此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...

  7. spring&plus;hibernate&plus;struts2零配置整合

    说句实话,很久都没使用SSH开发项目了,但是出于各种原因,再次记录一下整合方式,纯注解零配置. 一.前期准备工作 gradle配置文件: group 'com.bdqn.lyrk.ssh.study' ...

  8. Spring&plus;Hibernate&plus;struts2&plus;JPA 注解&plus;跨域&sol;&sol;完成手机端点击加载更多 下拉加载更多

    一.使用IDEA新建一个maven项目(student) 1.1.0编写pom文件,添加项目所需要的包 <?xml version="1.0" encoding=" ...

  9. Spring与Struts2整合

    Spring与Struts2为什么要整合呢? 把Action实例交给Spring来管理!! 1.单独测试Struts是否添加成功(jar包和配置文件),先单独测试,不要整合之后再测试,容易出问题 we ...

随机推荐

  1. 用EasyWebSvr搭建Axure本地访问地址-转载加完善

    1.下载之后解压到任意一个位置,可以是桌面(反正很小不占空间),如图2:: 图2 解压之后文件目录 2.将生成的原型放在EasyWebSvr根目录下的demo之中,如图3所示: 图3  原型文件放置目 ...

  2. 烂泥:KVM虚拟机克隆

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 上一篇文章介绍了有关KVM虚拟机快照的创建与恢复,这篇文章我们来介绍有关KVM虚拟机克隆. KVM虚拟机的克隆,我们可以分以下几步: 1. 使用virt ...

  3. jquerymobile知识点:动态Grid的绑定以及刷新

    下面jquerymobile是ajax动态绑定和刷新的例子.直接上图以及代码. 下面是实例代码: //初始绑定 function GetInitBind(PageIndex, PageSize, sq ...

  4. jquery dataTable 入门

    step1:切记要先引入jquery <link rel="stylesheet" type="text/css" href="C:\Users ...

  5. GDI编程

    图形设备接口(GDI)是一个可执行程序,它接受Windows应用程序的绘图请求(表现为GDI函数调用),并将它们传给相应的设备驱动程序,完成特定于硬件的输出,象打印机输出和屏幕输出.GDI负责Wind ...

  6. android开源系列:CircleImageView采用圆形控制它们的定义

    1.定义自己的圆形控制github住址:https://github.com/hdodenhof/CircleImageView 基本的类: package de.hdodenhof.circleim ...

  7. static 变量(静态变量)

    在C++的面向对象编程中,static还可以加在类的数据成员或成员函数之前.这样定义的数据成员或成员函数就被类所拥有,而不再属于类的对象. #include <iostream> usin ...

  8. bond-vlan-bridge

    拓扑介绍 Eth-Trunk5 down down 0% 0% 0 0 10GE1/0/5 down down 0.01% 0.01% 0 0 10GE2/0/5 down down 0.01% 0% ...

  9. Effective Java 第三版——83&period; 明智谨慎地使用延迟初始化

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  10. jQuery validator plugin之概要

    jQuery validator 主页 github地址 demo学习 效果: Validate forms like you've never validated before! 自定义Valida ...