博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SSH2环境搭建
阅读量:7195 次
发布时间:2019-06-29

本文共 12078 字,大约阅读时间需要 40 分钟。

一、创建SSH2项目,导入Struts2并测试。

 

 

web.xml

index.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action

default.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
default pagedefault page

UserAction.java

package com.cwq.action;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {    @Override    public String execute() throws Exception {        // TODO Auto-generated method stub        //return super.execute();        return SUCCESS;    }    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub    }}

struts.xml

default.jsp

发布项目,启动Tomcat,运行结果:

 二、导入Spring包,配置数据源

三,Spring与Hibernate整合

在web.xml中加入

<!-- 必须放在顶上 -->

 <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <!-- 对Spring容器进行实例化 -->
 <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
index.jsp
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action

applicationContext.xml(自动生成)

org.hibernate.dialect.MySQLDialect

测试,发布,启动Tomcat,如果启动成功则整合成功。

 四、根据数据库反向生成代码

  

 

Users.java

package com.cwq.model;import java.sql.Timestamp;/** * Users entity. @author MyEclipse Persistence Tools */public class Users implements java.io.Serializable {    // Fields    private Integer id;    private String username;    private String password;    private Timestamp birthdate;    // Constructors    /** default constructor */    public Users() {}    /** full constructor */    public Users(String username, String password, Timestamp birthdate) {        this.username = username;        this.password = password;        this.birthdate = birthdate;    }    // Property accessors    public Integer getId() {        return this.id;    }    public void setId(Integer id) {        this.id = id;    }    public String getUsername() {        return this.username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return this.password;    }    public void setPassword(String password) {        this.password = password;    }    public Timestamp getBirthdate() {        return this.birthdate;    }    public void setBirthdate(Timestamp birthdate) {        this.birthdate = birthdate;    }}

user.hbm.xml

UserDao.java

package com.cwq.dao;import java.sql.Timestamp;import java.util.List;import org.hibernate.LockMode;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.ApplicationContext;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.cwq.model.Users;/** * A data access object (DAO) providing persistence and search support for Users * entities. Transaction control of the save(), update() and delete() operations * can directly support Spring container-managed transactions or they can be * augmented to handle user-managed Spring transactions. Each of these methods * provides additional information for how to configure it for the desired type * of transaction control. * @see com.cwq.model.Users * @author MyEclipse Persistence Tools */public class UsersDAO extends HibernateDaoSupport {    private static final Logger log = LoggerFactory.getLogger(UsersDAO.class);    // property constants    public static final String USERNAME = "username";    public static final String PASSWORD = "password";    protected void initDao() {        // do nothing    }    public void save(Users transientInstance) {        log.debug("saving Users instance");        try {            getHibernateTemplate().save(transientInstance);            log.debug("save successful");        } catch (RuntimeException re) {            log.error("save failed", re);            throw re;        }    }    public void delete(Users persistentInstance) {        log.debug("deleting Users instance");        try {            getHibernateTemplate().delete(persistentInstance);            log.debug("delete successful");        } catch (RuntimeException re) {            log.error("delete failed", re);            throw re;        }    }    public Users findById(java.lang.Integer id) {        log.debug("getting Users instance with id: " + id);        try {            Users instance = (Users) getHibernateTemplate().get("com.cwq.model.Users", id);            return instance;        } catch (RuntimeException re) {            log.error("get failed", re);            throw re;        }    }    public List findByExample(Users instance) {        log.debug("finding Users instance by example");        try {            List results = getHibernateTemplate().findByExample(instance);            log.debug("find by example successful, result size: " + results.size());            return results;        } catch (RuntimeException re) {            log.error("find by example failed", re);            throw re;        }    }    public List findByProperty(String propertyName, Object value) {        log.debug("finding Users instance with property: " + propertyName + ", value: " + value);        try {            String queryString = "from Users as model where model." + propertyName + "= ?";            return getHibernateTemplate().find(queryString, value);        } catch (RuntimeException re) {            log.error("find by property name failed", re);            throw re;        }    }    public List findByUsername(Object username) {        return findByProperty(USERNAME, username);    }    public List findByPassword(Object password) {        return findByProperty(PASSWORD, password);    }    public List findAll() {        log.debug("finding all Users instances");        try {            String queryString = "from Users";            return getHibernateTemplate().find(queryString);        } catch (RuntimeException re) {            log.error("find all failed", re);            throw re;        }    }    public Users merge(Users detachedInstance) {        log.debug("merging Users instance");        try {            Users result = (Users) getHibernateTemplate().merge(detachedInstance);            log.debug("merge successful");            return result;        } catch (RuntimeException re) {            log.error("merge failed", re);            throw re;        }    }    public void attachDirty(Users instance) {        log.debug("attaching dirty Users instance");        try {            getHibernateTemplate().saveOrUpdate(instance);            log.debug("attach successful");        } catch (RuntimeException re) {            log.error("attach failed", re);            throw re;        }    }    public void attachClean(Users instance) {        log.debug("attaching clean Users instance");        try {            getHibernateTemplate().lock(instance, LockMode.NONE);            log.debug("attach successful");        } catch (RuntimeException re) {            log.error("attach failed", re);            throw re;        }    }    public static UsersDAO getFromApplicationContext(ApplicationContext ctx) {        return (UsersDAO) ctx.getBean("UsersDAO");    }}

applicationContext.xml

org.hibernate.dialect.MySQLDialect
com/cwq/model/Users.hbm.xml

 五、Struts整合Spring

UserAction.java

package com.cwq.action;import java.util.List;import com.cwq.dao.UsersDAO;import com.cwq.model.Users;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {        private List
users = null; private UsersDAO userDao; public void setUserDao(UsersDAO userDao) { this.userDao = userDao; } @Override public String execute() throws Exception { // TODO Auto-generated method stub //return super.execute(); users = userDao.findAll(); return SUCCESS; } public List
getUsers() { return users; } public void setUsers(List
users) { this.users = users; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub }}

applicationContext.xml

加入

<bean id="userAction" class="com.cwq.action.UserAction">

  <property name="userDao"><!-- 依赖注入(单例),在UserAction中必须要有一个userDao的属性有其set方法 -->
   <ref bean="UsersDAO"/>
  </property>
 </bean>

org.hibernate.dialect.MySQLDialect
com/cwq/model/Users.hbm.xml

 struts.xml

加入

<!-- 由Spring来代理控制层 -->

 <constant name="struts.objectFactory" value="spring" />

default.jsp

default.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
default pagedefault page

${user.username}--${user.password}--${user.birthdate}

 如果要指定进入某个action加入下面代码

 <script type="text/javascript">

  document.location.href = "showUsers.action";
 </script>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              My JSP 'index.jsp' starting page    
This is my JSP page.

 

 

转载地址:http://vevkm.baihongyu.com/

你可能感兴趣的文章
Mermaid js与流程图、甘特图..
查看>>
java 调度框架quartz
查看>>
hadoop exit code 退出码含义
查看>>
[C#基础知识系列]专题十:全面解析可空类型
查看>>
什么是.Net的异步机制(线程间通信) - step 5
查看>>
Lambda应用设计模式
查看>>
解决svn异常报错“”cleanup failed to process the following paths …… previous operation has not finished”...
查看>>
富文本框--FreeTextBox的使用
查看>>
koa2使用阿里云oss的nodejs sdk实现上传图片
查看>>
简单select(2)
查看>>
pandas基础学习
查看>>
用实例一步步教你写Jquery插件
查看>>
Qt资源整理ING
查看>>
看看checksec
查看>>
1. Two Sum
查看>>
MySQL基础之 标准模式通配符
查看>>
聊一聊python的单例模式
查看>>
第十一篇、RxSwift
查看>>
复分析学习9——全纯函数各阶导数在紧集上的一致估计
查看>>
run_test() 验证平台的入口
查看>>