ITPub博客

首页 > 应用开发 > Java > 完整的SSH案例

完整的SSH案例

原创 Java 作者:fengwuying121 时间:2017-10-21 19:57:40 0 删除 编辑


  1. 目录结构

  2. action类


  1. DeleteUserAction


 


package action;


 


import service.UserService;


import bean.User;


 


import com.opensymphony.xwork2.ActionSupport;


 


 


public class DeleteUserAction extends ActionSupport {


         /**


     *


     */


         private static final long serialVersionUID = 1L;


         private User user = new User();


         private UserService userService;


 


         public User getUser() {


                   return user;


         }


 


         public void setUser(User user) {


                   this.user = user;


         }


 


         public UserService getUserService() {


                   return userService;


         }


 


         public void setUserService(UserService userService) {


                   this.userService = userService;


         }


 


         @Override


         public String execute() throws Exception {


                   System.out.println("-------UserAction.deleteUser-----------"


                                     + user.getFirstName());


                   this.userService.delete(user);


                   return "SUCCESS";


         }


}


 


  1.  ListUserAction


package action;


import java.util.Map;


import com.opensymphony.xwork2.ActionContext;


import com.opensymphony.xwork2.ActionSupport;


import service.UserService;


public class ListUserAction extends ActionSupport {


    private static final long serialVersionUID = 1L;


    private UserService userService;


    public UserService getUserService() {


        return userService;


    }


    public void setUserService(UserService userService) {


        this.userService = userService;


    }


    @SuppressWarnings("unchecked")


    @Override


    public String execute() throws Exception {


        System.out.println("-------UserAction.ListUser-----------");


        Map request = (Map) ActionContext.getContext().get("request");


        request.put("list", this.userService.findAllUsers());


        System.out.println(3);


        return "SUCCESS";


    }


}


 


 


  1. SaveUserAction


package action;


import service.UserService;


import bean.User;


import com.opensymphony.xwork2.ActionSupport;


public class SaveUserAction extends ActionSupport {


    private static final long serialVersionUID = 1L;


    private User user;


    private UserService userService;


    public UserService getUserService() {


        return userService;


    }


    public void setUserService(UserService userService) {


        this.userService = userService;


    }


    public User getUser() {


        return user;


    }


    public void setUser(User user) {


        this.user = user;


    }


    @Override


    public String execute() throws Exception {


//      调用service相关的方法,完成实际的业务处理


        System.out.println("-------UserAction.SaveUser-----------"+user.getFirstName());


        this.userService.doCreateUser(user);


        return "SUCCESS";


    }


}


 


  1. UpdatePUser


package action;


 


import java.util.Map;


import service.UserService;


import bean.User;


import com.opensymphony.xwork2.ActionContext;


import com.opensymphony.xwork2.ActionSupport;


public class UpdatePUser extends ActionSupport {


    private static final long serialVersionUID = 1L;


    private User user=new User();


    private UserService userService;


    public User getUser() {


        return user;


    }


    public void setUser(User user) {


        this.user = user;


    }


    public UserService getUserService() {


        return userService;


    }


    public void setUserService(UserService userService) {


        this.userService = userService;


    }


    @Override


    public String execute() throws Exception {


        System.out.println("-------UserAction.UpdatePUser-----------"+user.getId());


        Map request = (Map) ActionContext.getContext().get("request");


        request.put("list", this.userService.findUserById(user.getId()));


        return "SUCCESS";


    }


}


 


  1. UpdateUserAction


package action;


 


import service.UserService;


import bean.User;


import com.opensymphony.xwork2.ActionSupport;


public class UpdateUserAction extends ActionSupport {


         private User user = new User();


         private UserService userService;


         public User getUser() {


                   return user;


         }


         public void setUser(User user) {


                   this.user = user;


         }


         public UserService getUserService() {


                   return userService;


         }


         public void setUserService(UserService userService) {


                   this.userService = userService;


         }


         @Override


         public String execute() throws Exception {


                   System.out.println("-------UserAction.UpdateUser-----------"


                                     + user.getFirstName());


                   this.userService.update(this.user);


                   return "SUCCESS";


         }


}


 


  1. Bean类


package bean;


 


public class User { 


    private int id; 


    private String firstName; 


    private String lastName; 


    private int age; 


    public int getId() { 


        return id; 


    } 


    public void setId(int id) { 


        this.id = id; 


    } 


    public String getFirstName() { 


        return firstName; 


    } 


    public void setFirstName(String firstName) { 


        this.firstName = firstName; 


    } 


    public String getLastName() { 


        return lastName; 


    } 


    public void setLastName(String lastName) { 


        this.lastName = lastName; 


    } 


    public int getAge() { 


        return age; 


    } 


    public void setAge(int age) { 


        this.age = age; 


    } 


    @Override 


    public String toString() { 


        return "ID: " + id + "FirstName: " + firstName; 


    } 


}


 


 


  1. User.hbm.xml


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


<!DOCTYPE hibernate-mapping PUBLIC


    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"


    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">


<hibernate-mapping>


    <class name="bean.User" table="tb_users">


        <id name="id"></id>


        <property name="firstName" />


        <property name="lastName" />


        <property name="age" />


    </class>


</hibernate-mapping> 


 


  1. Dao类

    package dao;

     

    import java.util.List;

    import bean.User;

    public interface UserDAO {

    public void doCreateUser(User user);

    public List<User> findAllUsers();

    public void removeUser(User user);

    public void updateUser(User user);

    public List<User> findUserById(int id);

     

    }

     

    package dao;

     

    import java.util.List;

    import org.hibernate.Query;

    import org.hibernate.Session;

    import org.hibernate.SessionFactory;

    import bean.User;

    public class UserDAOImpl implements UserDAO {

     

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {

               this.sessionFactory = sessionFactory;

    }

     

    public Session getSession() {

               return sessionFactory.getCurrentSession();

    }

     

    @Override

    public void doCreateUser(User user) {

               // TODO Auto-generated method stub

               System.out.println("-------UserDaoImp.doCreateUser-----------"

                                 + user.getFirstName());

               System.out.println("id" + user.getId());

               System.out.println("firstname" + user.getFirstName());

               System.out.println("lastname" + user.getLastName());

               System.out.println("age" + user.getAge());

               getSession().save(user);

    }

     

    @Override

    public List<User> findAllUsers() {

               // TODO Auto-generated method stub

               String hql = "from User u order by u.id asc";

               System.out.println(1);

               Query query = getSession().createQuery(hql);

               System.out.println(2);

               List<User> list = query.list();

               return list;

    }

     

    @Override

    public void removeUser(User user) {

               // TODO Auto-generated method stub

               System.out.println("-------UserDaoImp.removeUser-----------"

                                 + user.getFirstName());

               getSession().delete(user);

    }

     

    @Override

    public void updateUser(User user) {

               // TODO Auto-generated method stub

               System.out.println("-------UserDaoImp.updateUser-----------"

                                 + user.getFirstName());

               getSession().update(user);

    }

     

    @Override

    public List<User> findUserById(int id) {

               // TODO Auto-generated method stub

               System.out.println("-------UserDaoImp.findUserById-----------" + id);

               String hql = "from User u where u.id=:id order by u.id asc";

               Query query = getSession().createQuery(hql);

               query.setInteger("id", id);

               List<User> list = query.list();

               return list;

    }

     

    }

     

     

  2. Service类

    package service;

     

    import java.util.List;

    import bean.User;

    public interface UserService {

    public void doCreateUser(User user);

        public List<User> findAllUsers();

        public void delete(User user);

        public void update(User user);

        public List<User> findUserById(int id);

     

    }

     

    package service;

     

    import java.util.List;

    import dao.UserDAO;

    import bean.User;

    public class UserServiceImpl implements UserService {

    private UserDAO dao;

        public UserDAO getDao() {

            return dao;

        }

        public void setDao(UserDAO dao) {

            this.dao = dao;

        }

        public void doCreateUser(User user) {

        System.out.println("-------UserService.doCreateUser-----------"+user.getFirstName());

            this.dao.doCreateUser(user);

        }

        public List<User> findAllUsers() {

            System.out.println("-------UserService.findAllUsers-----------");

            return this.dao.findAllUsers();

        }

        public void delete(User user) {

          System.out.println("-------UserService.deleteUser-----------"+user.getFirstName());

            this.dao.removeUser(user);

        }

        public void update(User user) {

         System.out.println("-------UserService.updateUser-----------"+user.getFirstName());

            this.dao.updateUser(user);

        }

        public List<User> findUserById(int id) {

            System.out.println("-------UserService.findUserById-----------"+id);

            return this.dao.findUserById(id);

        }

     

     

    }

     

  3. 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: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.xsd


    http://www.springframework.org/schema/context


    http://www.springframework.org/schema/context/spring-context.xsd


    http://www.springframework.org/schema/aop


    http://www.springframework.org/schema/aop/spring-aop.xsd


    http://www.springframework.org/schema/tx


    http://www.springframework.org/schema/tx/spring-tx.xsd">


 


 


    <!-- 引入外部属性文件. -->


    <context:property-placeholder location="classpath:jdbc.properties" />


    <!-- 配置c3p0连接池 -->


    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">


        <property name="driverClass" value="${jdbc.driver}" />


        <property name="jdbcUrl" value="${jdbc.url}" />


        <property name="user" value="${jdbc.user}" />


        <property name="password" value="${jdbc.password}" />


    </bean>


 


    <!-- 零障碍整合 在spring配置文件中引入hibernate的配置文件 -->


    <bean id="sessionFactory"


        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">


        <!-- 管理连接池 -->


        <property name="dataSource" ref="dataSource"></property>


        <!-- hibernate属性 -->


        <property name="hibernateProperties">


            <props>


                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>


                <prop key="hibernate.show_sql">true</prop>


                <prop key="hibernate.format_sql">true</prop>


                <prop key="hibernate.hbm2ddl.auto">update</prop>


                <prop key="hibernate.connection.autocommit">false</prop>


            </props>


        </property>


       


        <property name="mappingResources">


            <list>


                <value>bean/User.hbm.xml</value>


            </list>


        </property>


        <!-- <property name="mappingDirectoryLocations">


            <list>


                <value>classpath:vo</value>


            </list>


        </property> -->


    </bean>


 


 


    <bean id="userDAO" class="dao.UserDAOImpl" scope="singleton">


        <property name="sessionFactory" ref="sessionFactory">


        </property>


    </bean>


 


    <bean id="userServiceTarget" class="service.UserServiceImpl"


        scope="singleton">


        <property name="dao" ref="userDAO" />


    </bean>


 


    <bean id="userService"


        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">


        <property name="target" ref="userServiceTarget">


        </property>


        <property name="transactionManager" ref="transactionManager">


        </property>


        <property name="transactionAttributes">


            <props>


                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>


                <prop key="*">PROPAGATION_REQUIRED</prop>


            </props>


        </property>


    </bean>


 


    <bean id="saveUserAction" class="action.SaveUserAction" scope="prototype">


        <property name="userService" ref="userService"></property>


    </bean>


    <bean id="listUserAction" class="action.ListUserAction" scope="prototype">


        <property name="userService" ref="userService"></property>


    </bean>


    <bean id="removeUserAction" class="action.DeleteUserAction" scope="prototype">


        <property name="userService" ref="userService"></property>


    </bean>


    <bean id="updatePUserAction" class="action.UpdatePUser">


        <property name="userService" ref="userService"></property>


    </bean>


    <bean id="updateUserAction" class="action.UpdateUserAction" scope="prototype">


        <property name="userService" ref="userService"></property>


    </bean>


    <bean id="UpdatePUser" class="action.UpdatePUser" scope="prototype">


        <property name="userService" ref="userService"></property>


    </bean>


    <!-- 事务管理器 -->


    <bean id="transactionManager"


        class="org.springframework.orm.hibernate3.HibernateTransactionManager">


        <property name="sessionFactory" ref="sessionFactory" />


    </bean>


    <!-- 注解开户事务 -->


    <tx:annotation-driven transaction-manager="transactionManager" />


</beans>


 


 


  1. jdbc.properties

    jdbc.user=scott

    jdbc.password=tiger

    jdbc.driver=oracle.jdbc.driver.OracleDriver

    jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl

     

  2. 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>


    <constant name="struts.devMode" value="false" />


    <package name="default" namespace="/" extends="struts-default">


        <action name="saveUser" class="saveUserAction">


            <result name="SUCCESS" type="redirectAction">listUser</result>


        </action>


        <action name="listUser" class="listUserAction">


            <result name="SUCCESS">/list.jsp</result>


        </action>


        <action name="deleteUser" class="removeUserAction">


            <result name="SUCCESS" type="redirectAction">listUser</result>


        </action>


        <action name="updatePUser" class="updatePUserAction">


            <result name="SUCCESS">/update.jsp</result>


        </action>


        <action name="updateUser" class="updateUserAction">


            <result name="SUCCESS" type="redirectAction">listUser</result>


        </action>


        <action name="findUser" class="UpdatePUser">


            <result name="SUCCESS">/list.jsp</result>


        </action>


    </package>


</struts>


 


  1. index.jsp


 


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<%@ taglib prefix="s" uri="/struts-tags" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html>


  <head>


    <title>My JSP 'index.jsp' starting page</title>


  </head>


  <body>


    <h1><font color="red">Operation List</font></h1>


    <s:a href="update.jsp">Update User</s:a><br/>


    <s:a href="Save.jsp">Save User</s:a><br/>


    <s:a href="listUser.action">List Users</s:a>


    <s:form action="findUser">


        <s:textfield name="user.id" label="id"></s:textfield>


        <s:submit></s:submit>


    </s:form>


  </body>


</html>


 


  1. list.jsp


 


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<%@ taglib prefix="s" uri="/struts-tags"%>


 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html>


  <head> 


    <title>My JSP 'list.jsp' starting page</title>


    <script type="text/javascript">


        function del(){


            if(confirm("Are you sure?")){


                return true;


            }


            return false;


        }


    </script>


  </head>


 


  <body>


    <h1><font color="red">Users List</font></h1>


    <s:a href="/ssh_learn/index.jsp">返回</s:a>


    <table border="1" width="80%" align="center">


        <tr>


            <th>序号</th>


            <th>姓</th>


            <th>名</th>


            <th>年龄</th>


            <th>删除</th>


            <th>更新</th>


        </tr>


 


        <s:iterator value="#request.list" id="us">


            <tr>


                <td>


                    <s:property value="#us.id"/>


                </td>


                <td>


                    <s:property value="#us.firstName"/>


                </td>


                <td>


                    <s:property value="#us.lastName"/>


                </td>


                <td>


                    <s:property value="#us.age"/>


                </td>


                <td align="center">


                    <s:a href="deleteUser.action?user.id=%{#us.id}" onclick="return del()">Delete</s:a>


                </td>


                <td align="center">


                    <s:a href="updatePUser.action?user.id=%{#us.id}">Update</s:a>      


                </td>


            </tr>


        </s:iterator>


    </table>


 


  </body>


</html>


 


  1. Save.jsp


 


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<%@ taglib prefix="s" uri="/struts-tags"%>


 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html>


<head>


 


<title>My JSP 'update.jsp' starting page</title>


 


</head>


 


<body>


    <h1>


        <font color="red">Save User</font>


    </h1>


 


    <s:form action="saveUser">


        <table>


            <tr>


                <td><s:hidden name="user.id" value="%{user.id}"></s:hidden></td>


            </tr>


 


            <tr>


                <td><s:textfield name="user.firstName"


                        value="%{user.firstName}" label="First Name"></s:textfield></td>


            </tr>


 


            <tr>


                <td><s:textfield name="user.lastName" value="%{user.lastName}"


                        label="Last Name"></s:textfield></td>


            </tr>


 


            <tr>


                <td><s:textfield name="user.age" value="%{user.age}"


                        label="Age"></s:textfield></td>


            </tr>


 


            <tr>


                <td><s:submit></s:submit></td>


            </tr>


        </table>


    </s:form>


 


</body>


</html>


 


  1. update.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<%@ taglib prefix="s" uri="/struts-tags"%>


 


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<html>


<head>


 


<title>My JSP 'update.jsp' starting page</title>


</head>


<body>


    <h1>


        <font color="red">Update User</font>


    </h1>


    <s:form action="updateUser">


        <table>


            <tr>


                <td><s:hidden name="user.id" value="%{user.id}"></s:hidden></td>


            </tr>


 


            <tr>


                <td><s:textfield name="user.firstName"


                        value="%{user.firstName}" label="First Name"></s:textfield></td>


            </tr>


 


            <tr>


                <td><s:textfield name="user.lastName" value="%{user.lastName}"


                        label="Last Name"></s:textfield></td>


            </tr>


 


            <tr>


                <td><s:textfield name="user.age" value="%{user.age}"


                        label="Age"></s:textfield></td>


            </tr>


 


            <tr>


                <td><s:submit></s:submit></td>


            </tr>


        </table>


    </s:form>


 


</body>


</html>


 


  1. WEB-INF.xml


 


<?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">


 


    <context-param>


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


        <param-value>/WEB-INF/classes/applicationContext.xml </param-value>


    </context-param>


    <welcome-file-list>


        <welcome-file>index.jsp</welcome-file>


    </welcome-file-list>


    <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>


    <listener>


        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>


    </listener>


</web-app>


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30180323/viewspace-2146215/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2015-03-27

  • 博文量
    61
  • 访问量
    44775