首页 > 其他分享 >struts 2+hibernate 3+spring基本套路小结

struts 2+hibernate 3+spring基本套路小结

时间:2022-12-09 21:00:35浏览次数:37  
标签:hibernate http spring struts User import org public user

任何东西都是有一定的套路和规律的,struts 2+hibernate 3+spring也一样,struts2 比struts1爽很多了,但目前唯一觉得不爽的

是struts2 居然没什么好的IDE工具去支持设计之,比如DW8,希望以后有所改进.

这里,以一个简单的用户注册,登陆去说明这三者之间的简单组合关系,其他都可以按这样的去做:

1 建立domain层

建立包domain,里面有个user.java

public class User {

private Long id;

private String userName;

private String password;

//getter,setter.....


2)建立user.hbm.xml

省略

3) 建立dao层

   建立dao包,有userdao.java


import java.util.List;
import liao.domain.User;
public interface UserDAO {
public User findById(long id);
public void save(User user);
public List<User> findByUserNameAndPassword(String userName,String password);
public List<User> findByUserName(String userName);
} 4 建立userdaoimpl.java


import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import liao.dao.UserDAO;
import liao.domain.User;public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{
public User findById(long id) {
return (User)getHibernateTemplate().get(User.class, id);
} public List<User> findByUserName(String userName) {
return getHibernateTemplate().find("from User where userName=?",new String[]{userName});
} public List<User> findByUserNameAndPassword(String userName, String password) {
return getHibernateTemplate().find("from User where userName=? and password=?",new String[]{userName,password});
} public void save(User user) {
getHibernateTemplate().saveOrUpdate(user);
}}
5 建立sevrice层,建立好service包

import edu.jlu.fuliang.domain.User;

public interface UserManageService {
public void register(User user);
public boolean login(User user);
} 6 service的实现

  
import java.util.List;
import liao.Service.UserManageService;
import liao.dao.UserDAO;
import liao.domain.User;public class UserManageServiceImpl implements UserManageService{
private UserDAO userDAO;
public boolean login(User user) {
List<User> users = userDAO.findByUserNameAndPassword(user.getUserName(), user.getPassword()) ;
return users.size() != 0;
} public void register(User user) {
userDAO.save(user);
}

public UserDAO getUserDAO() {
return userDAO;
} public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}}
7 之后就是struts2的action层了

  首先是注册registeraction.java


import com.opensymphony.xwork2.ActionSupport;
import liao.Service.UserManageService;
import liao.domain.User;public class RegisterAction extends ActionSupport{
private User user;
private UserManageService userManageService;

public String execute()throws Exception{
userManageService.register(user);
return SUCCESS;
}

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public void setUserManageService(UserManageService userManageService) {
this.userManageService = userManageService;
}
} 登陆的loginaction.java


public class LoginAction extends ActionSupport{
private User user;
private UserManageService userManageService;

public String execute()throws Exception{
if(userManageService.login(user)){
ServletActionContext.getContext().getSession().put("user",user);
return SUCCESS;
}
return LOGIN;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public void setUserManageService(UserManageService userManageService) {
this.userManageService = userManageService;
}
} 8 配置好各类的applcaition.xml,建议分开几个来配置,避免全部放在一个xml

application-db.xml:比较传统,不说了

 application-dao.xml:

<bean id="userDAO" class="liao.dao.impl.UserDAOImpl">

<property name="sessionFactory">

<ref bean="sessionFactory"/>

</property>

</bean>

application-service.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: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/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="userManageService" class="liao.Service.impl.UserManageServiceImpl">

<property name="userDAO">

<ref bean="userDAO"/>

</property>

</bean>

<aop:config>

<aop:pointcut id="serviceOperation" expression="execution(* edu.jlu.fuliang.Service.impl.*ServiceImpl.*(..))"/>

<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>

</aop:config>



<tx:advice id="txAdvice">

<tx:attributes>

<tx:method name="get*" read-only="true"/>

<tx:method name="*"/>

</tx:attributes>

</tx:advice>



<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory">

<ref bean="sessionFactory"/>

</property>

</bean>

9 struts.xml

<action name="login" class="loginAction">

<result name="success">/uploadSong.jsp</result>

<result name="login">/login.jsp</result>

</action>



<action name="register" class="registerAction">

<result name="success" type="redirect-action">listSong</result>

<result name="input">/register.jsp</result>

</action>

10 web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">



<filter>

<filter-name> struts-cleanup </filter-name>

<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>

</filter>



<filter>

<filter-name>sitemesh</filter-name>

<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>

</filter>



<filter>

<filter-name>struts</filter-name>

<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

</filter>



<filter>

<filter-name>hibernateFilter</filter-name>

<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

</filter>



<filter-mapping>

<filter-name>struts-cleanup</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>





<filter-mapping>

<filter-name>sitemesh</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>





<filter-mapping>

<filter-name>struts</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>



<filter-mapping>

<filter-name>hibernateFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>



<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext-*.xml</param-value>

</context-param>



<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

</web-app>

标签:hibernate,http,spring,struts,User,import,org,public,user
From: https://blog.51cto.com/u_14230175/5926548

相关文章

  • SpringCloud学习 系列七、EurekaServer集群创建
    系列导航SpringCloud学习系列一、前言-为什么要学习微服务SpringCloud学习系列二、简介SpringCloud学习系列三、创建一个没有使用springCloud的服务提供者和消费......
  • SpringBoot中MongoDB聚合管道查询操作$facet$lookup$unwind$group
    前言、官方文档、MongoTemplate中的概念前言最近在做基于SpringBoot的MongoDB的聚合管道操作,JSON语句不难写,但是理清楚逻辑、顺序很麻烦,而且在Java(Springboot)上操作聚合......
  • spring mvc环境之数据库ORM组件Mybaits配置(九)
    spring引入Mybaits功能,还是把握住主要步骤,1.引入相关jar包(spring-jdbc、spring-orm,spring-aop、spring-tx(有orm就可以)、spring-oxm(无关)、mybatis、mybatis-spring);2......
  • Spring boot与Spring cloud 之间的关系
     Springboot与Springcloud之间的关系Springboot是Spring的一套快速配置脚手架,可以基于springboot快速开发单个微服务,SpringBoot,看名字就知道是Spring的......
  • SpringBoot单元测试类
    1、注解:@SpringBootTest:表示标注当前的类是一个测试类,不会随项目一起打包;@RunWith(SpringRunner.class):表示启动这个单元测试类(单元测试类是不能运行的),需要传递一个参数,必......
  • SSM整合(spring-springmvc-mybatis)之CRUD
    概述:加入功能:CRUD、分页、返回json、国际化配置了访问静态文件;配置了REST风格的代码;一、目录结构:目录: jar包说明:spring核心jar包:commons-logging-1.2.......
  • 随笔(五)『SpringBoot & MybatisPlus 代码生成(CRUD)』
    1、添加依赖<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency><depe......
  • springcloud alibaba nacos+sentinel
    Nacos数据模型(领域模型)Namespace:命名空间,对不同的环境进行隔离,比如隔离开发环境、测试环境和生产环境概念描述Group:分组,将若干个服务或者若干个配置集归为一组,通常习惯......
  • Spring Cloud OAuth2 + JWT
    1.微服务架构下统⼀认证思路基于Session的认证⽅式在分布式的环境下,基于session的认证会出现⼀个问题,每个应⽤服务都需要在session中存储⽤户身份信息,通过负载均衡将本地......
  • SpringBoot打war包,放Tomcat部署启动报错
    本人用的Jeecg-boot3.4.4最新版本打war包,按照官方文档war部署方式改了之后,启动tomcat就报如下错09-Dec-202215:08:01.902警告[localhost-startStop-1]org.apache.ca......