【版权声明:本文为新价值网原创,未经准许谢绝转载。如需转载,请务必在转载时注明本博客地址。】
一. 创建项目
二. 搭建struts-2.3.4.11.struts2必须的Jar包(放到WEB-INF/lib目录下):
2.配置struts2.3的过滤器
web.xml位置
Web.xml内容
- <?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>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <!-- 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>
- </web-app>
配置struts.xml(struts.xml在src目录下)
Struts.xml内容
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <package name="struts2" extends="struts-default">
- <action name="TestAction" class="com.xinvalue.action.TestAction">
- <result name="success">/test.jsp</result>
- </action>
- </package>
- </struts>
4.测试struts2配置
新建一个TestAction
TestAction.java
- package com.xinvalue.action;
- import com.opensymphony.xwork2.ActionSupport;
- public class TestAction extends ActionSupport {
- @Override
- public String execute() throws Exception {
- return super.execute();
- }
- }
新建一个测试页面
成功界面:
至此,struts2集成完毕!
整合Spring 3.2.3和Struts-2.3.4.11.
必须的jar包
在配置好的strutsjar的基础上,添加额外Struts jar包:
struts2-spring-plugin-2.3.4.1.jar
commons-logging-1.1.1.jar
Spring的jar包:
2.web.xml配置
Web.xml
- <?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>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <!-- 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工厂监听器 -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 告知spring context config location 的存储位置 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
- </context-param>
- </web-app>
3.spring的applicationContext.xml配置
applicationContext.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:p="http://www.springframework.org/schema/p"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- </beans>
整合Hibernate4.1.9
1.必需jar包
添加Hibernate jar文件
Hibernate压缩包中的
lib/required/
Spring中的jar文件
数据库连接池支持文件
以及连接mysql的jar
mysql-connector-java-5.1.22-bin.jar
2.配置文件applicationContext.xml
applicationContext.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:p="http://www.springframework.org/schema/p"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- 数据库连接 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="url">
- <value>jdbc:mysql://localhost:3306/ssh2?characterEncoding=utf8</value>
- </property>
- <property name="username">
- <value>root</value>
- </property>
- <property name="password">
- <value></value>
- </property>
- </bean>
- <!--Hibernate的Spring配置 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <!-- 数据库连接 -->
- <property name="dataSource">
- <ref local="dataSource" />
- </property>
- <!-- hibernate自身属性 -->
- <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.MySQL5Dialect</prop>
- <!-- 解决no session found -->
- <prop key="hibernate.current_session_context_class">thread</prop>
- </props>
- </property>
- <!-- 映射文件 -->
- <property name="annotatedClasses">
- <list>
- <value>com.xinvalue.bean.User</value>
- </list>
- </property>
- </bean>
- <!-- 用户Dao -->
- <bean id="userDao" class="com.xinvalue.dao.impl.UserDaoImpl"
- scope="singleton">
- <property name="sessionFactory">
- <ref local="sessionFactory" />
- </property>
- </bean>
- <!-- 用户Service -->
- <bean id="userService" class="com.xinvalue.service.impl.UserServiceImpl"
- scope="singleton">
- <property name="userDao">
- <ref local="userDao" />
- </property>
- </bean>
- <!-- 用户Action -->
- <bean id="saveUserAction" class="com.xinvalue.action.SaveUserAction"
- scope="prototype">
- <property name="userService">
- <ref local="userService" />
- </property>
- </bean>
- </beans>
创建测试数据库
Sql代码
- CREATE DATABASE `ssh2` ;
- USE `ssh2`;
- DROP TABLE IF EXISTS `users`;
- CREATE TABLE `users` (
- `userid` int(11) NOT NULL AUTO_INCREMENT,
- `username` varchar(20) DEFAULT NULL,
- `userpwd` varchar(20) DEFAULT NULL,
- PRIMARY KEY (`userid`)
- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
- insert into `users`(`userid`,`username`,`userpwd`) values (1,'terwer','123456');
创建bean
User.java
- package com.xinvalue.bean;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- import javax.persistence.Table;
- @Entity
- @Table(name = "users")
- public class User {
- @Id
- private int userId;
- @Column(name = "username")
- private String username;
- @Column(name = "userpwd")
- private String userpwd;
- public int getUserId() {
- return userId;
- }
- public void setUserId(int userId) {
- this.userId = userId;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getUserpwd() {
- return userpwd;
- }
- public void setUserpwd(String userpwd) {
- this.userpwd = userpwd;
- }
- }
创建dao
UserDao.java
- package com.xinvalue.dao;
- import java.util.List;
- import com.xinvalue.bean.User;
- public interface UserDao {
- public List<User> queryAllUsers();
- boolean saveUser(User user);
- }
UserDaoImpl.java
- package com.xinvalue.dao.impl;
- import java.util.List;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import com.xinvalue.bean.User;
- import com.xinvalue.dao.UserDao;
- public class UserDaoImpl implements UserDao {
- private SessionFactory sessionFactory;
- public SessionFactory getSessionFactory() {
- return sessionFactory;
- }
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- @Override
- public List<User> queryAllUsers() {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public boolean saveUser(User user) {
- Session session = sessionFactory.getCurrentSession();
- Transaction tx = session.beginTransaction();
- try {
- session.save(user);
- tx.commit();
- return true;
- } catch (Exception e) {
- if (e != null) {
- tx.rollback();
- }
- }
- return false;
- }
- }
创建Service
UserService.java
- package com.xinvalue.service;
- import java.util.List;
- import com.xinvalue.bean.User;
- public interface UserService {
- public List<User> queryAllUsers();
- boolean saveUser(User user);
- }
UserServiceImpl.java
- package com.xinvalue.service.impl;
- import java.util.List;
- import com.xinvalue.bean.User;
- import com.xinvalue.dao.UserDao;
- import com.xinvalue.service.UserService;
- public class UserServiceImpl implements UserService {
- private UserDao userDao;
- public UserDao getUserDao() {
- return userDao;
- }
- public void setUserDao(UserDao userDao) {
- this.userDao = userDao;
- }
- @Override
- public List<User> queryAllUsers() {
- return userDao.queryAllUsers();
- }
- @Override
- public boolean saveUser(User user) {
- return userDao.saveUser(user);
- }
- }
创建Action
SaveUserAction.java
- package com.xinvalue.action;
- import com.opensymphony.xwork2.ActionSupport;
- import com.xinvalue.bean.User;
- import com.xinvalue.service.UserService;
- public class SaveUserAction extends ActionSupport {
- private UserService userService;
- private String username;
- private String userpwd;
- public UserService getUserService() {
- return userService;
- }
- public void setUserService(UserService userService) {
- this.userService = userService;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getUserpwd() {
- return userpwd;
- }
- public void setUserpwd(String userpwd) {
- this.userpwd = userpwd;
- }
- @Override
- public String execute() throws Exception {
- User user = new User();
- user.setUsername(this.getUsername());
- user.setUserpwd(this.getUserpwd());
- boolean status=userService.saveUser(user);
- System.out.println(status);
- return SUCCESS;
- }
- }
创建测试页面:
addUser.jsp
- <body>
- <s:form action="SaveUserAction.action">
- <s:textfield name="username" label="用户名"/>
- <s:password name="userpwd" label="密码"/>
- <s:submit label="注册"/>
- </s:form>
- </body>
成功界面:
至此,全部整合完毕!
附:项目所有jar包下载:ss2-libs
如有大家有什么问题,可以给我留言,我会抽时间一一解答。
笔者微博:@LeaveBugsAway欢迎叨扰。