struts2+spring+hibernate s2sh结合方式框架搭建开发

来源:互联网 发布:mac如何删除word软件 编辑:程序博客网 时间:2024/06/06 08:47

struts2+spring+hibernate s2sh结合方式框架搭建开发

1.使用全xml配置开发s2sh,需要配置hibernate,struts,

创建mysql表:

CREATE TABLE `user` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `name` varchar(10) DEFAULT NULL,  `phone` varchar(20) DEFAULT NULL,  `age` int(11) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

2.新建一个web工程,导入jar包,web.xml配置spring,struts启动:

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" 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_3_0.xsd">  <display-name></display-name>  <!-- spring web启动方式  start--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/applicationContext.xml</param-value></context-param><!-- spring web启动方式  end--><!-- 配置struts2启动过滤器 start -->  <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>  <!-- 配置struts2启动过滤器 end -->  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>
3.struts.xml配置struts

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><!-- Struts默认从spring工厂中寻找对象 --><constant name="struts.objectFactory" value="spring"></constant><package name="base" extends="struts-default"><action name="userAction" class="userAction"><result name="list">/jsp/user/list_user.jsp</result><!-- 重定向到action的请求中 --><result name="listAction" type="redirectAction">userAction!findUserList.action</result></action></package></struts>
4.applicationContext.xml配置spring和hibernate,比半注解多了控制反转的配置

<?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-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean name="userAction" class="com.zl.test1.action.UserAction"><property name="userService" ref="userService"/></bean><!-- servie --><bean id="userService" class="com.zl.test1.service.UserServiceImpl"><property name="userDao"><ref bean="userDao"/></property></bean><!-- dao --><bean id="userDao" class="com.zl.test1.dao.impl.UserDaoImpl"><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean><!-- HIBERNATE会话工厂 --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="hibernateProperties"><props><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hbm2ddl.auto">create</prop></props></property><property name="mappingResources"><list><value>com/zl/test1/entity/User.hbm.xml</value></list></property></bean><!-- 数据源 --><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/ssh?useUnicode=true&characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="tiger"/></bean></beans> 
5.view层:

5.1  list显示全部jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'list_user.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page">  </head>    <body>   <table border="1" align="center" width="80%"><tr><td>ID</td><td>name</td><td>age</td><td>phone</td></tr><s:iterator value="#attr.userList" var="us"><tr><td>${us.id}</td><td>${us.name}</td><td>${us.age}</td><td>${us.phone}</td></tr></s:iterator>   </table>  </body></html> 
5.2  save保存jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'save_user.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><form method="post" action="userAction!saveUser.action"><p> 姓名:<input type="text" name="user.name"></p><p> 年龄:<input type="text" name="user.age"></p><p> 电话:<input type="text" name="user.phone"></p><p> <input type="submit" value="提交"></p></form></body></html>
6.controller层:

package com.zl.test1.action;import java.util.List;import java.util.Map;import org.apache.struts2.interceptor.RequestAware;import com.opensymphony.xwork2.ActionSupport;import com.zl.test1.entity.User;import com.zl.test1.service.IUserService;public class UserAction extends ActionSupport implements RequestAware {private User user;IUserService userService;public void setUserService(IUserService userService) {this.userService = userService;}public String saveUser() {System.out.println(user);userService.saveUser(user);return "listAction";}public String findUserList() {List<User> userList = userService.findUserList();reqMap.put("userList", userList);return "list";}public User getUser() {return user;}public void setUser(User user) {this.user = user;}Map<String, Object> reqMap;@Overridepublic void setRequest(Map<String, Object> reqMap) {this.reqMap = reqMap;}}
7.service层:

package com.zl.test1.service;import java.util.List;import com.zl.test1.dao.IUserDao;import com.zl.test1.entity.User;/** * 业务实现类 *  * @author zenglong *  */public class UserServiceImpl implements IUserService {// 依赖外部注入IUserDao userDao;public void setUserDao(IUserDao userDao) {this.userDao = userDao;}public void saveUser(User user) {userDao.saveUser(user);}public List<User> findUserList() {return userDao.findUserList();}}
8.dao层

package com.zl.test1.dao.impl;import java.util.List;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.zl.test1.dao.IUserDao;import com.zl.test1.entity.User;public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {@Overridepublic List<User> findUserList() {return this.getHibernateTemplate().find("from User");}@Overridepublic void saveUser(User user) {this.getHibernateTemplate().save(user);}}
9.entity:

9.1:实体.hbm.xml: hibernate实体xml配置

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping><class name="com.zl.test1.entity.User" table="user"><id column="id" name="id"><generator class="identity"></generator></id><property name="name" type="string"></property><property name="age" type="int"></property><property name="phone" type="string"></property></class></hibernate-mapping>

9.2实体类:

package com.zl.test1.entity;public class User {private int id;private String name;private int age;private String phone;public User() {}public User(String name, int age, String phone) {super();this.name = name;this.age = age;this.phone = phone;}public User(int id, String name, int age, String phone) {super();this.id = id;this.name = name;this.age = age;this.phone = phone;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}}
10:浏览器输入地址结果如图:




源代码下载:点击打开链接
http://download.csdn.net/detail/qq_31968809/9772325






0 0
原创粉丝点击