zoukankan      html  css  js  c++  java
  • [转]SSH整合 简单的增删改查

     

    Database 使用 Mysql ,

    建立User 表

    1. CREATE TABLE `user` (  
    2.   `id` int(11) NOT NULL,  
    3.   `firstName` varchar(20) DEFAULT NULL,  
    4.   `lastName` varchar(20) DEFAULT NULL,  
    5.   `age` int(11) DEFAULT NULL,  
    6.   PRIMARY KEY (`id`)  
    7. )   

    所需jar包

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <classpath>  
    3.     <classpathentry kind="src" path="src"/>  
    4.     <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>  
    5.     <classpathentry kind="con" path="melibrary.com.genuitec.eclipse.j2eedt.core.MYECLIPSE_JAVAEE_5_CONTAINER"/>  
    6.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts2-spring-plugin-2.3.4.jar"/>  
    7.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/antlr-2.7.6.jar"/>  
    8.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/cglib-2.2.jar"/>  
    9.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-collections-3.1.jar"/>  
    10.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/dom4j-1.6.1.jar"/>  
    11.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ehcache-1.2.3.jar"/>  
    12.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ejb3-persistence.jar"/>  
    13.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate-annotations.jar"/>  
    14.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate-commons-annotations.jar"/>  
    15.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate-entitymanager.jar"/>  
    16.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate-validator.jar"/>  
    17.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/hibernate3.jar"/>  
    18.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/javassist-3.9.0.GA.jar"/>  
    19.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jta-1.1.jar"/>  
    20.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/log4j-1.2.14.jar"/>  
    21.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-api-1.5.8.jar"/>  
    22.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-log4j12-1.5.8.jar"/>  
    23.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.net.sf.cglib-2.2.0.jar"/>  
    24.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.aopalliance-1.0.0.jar"/>  
    25.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.apache.commons.fileupload-1.2.0.jar"/>  
    26.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.apache.commons.httpclient-3.1.0.jar"/>  
    27.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.apache.commons.logging-1.1.1.jar"/>  
    28.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.apache.log4j-1.2.15.jar"/>  
    29.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar"/>  
    30.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/com.springsource.org.codehaus.jackson.mapper-1.0.0.jar"/>  
    31.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.aop-3.0.1.RELEASE-A.jar"/>  
    32.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.asm-3.0.1.RELEASE-A.jar"/>  
    33.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.aspects-3.0.1.RELEASE-A.jar"/>  
    34.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.beans-3.0.1.RELEASE-A.jar"/>  
    35.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.context-3.0.1.RELEASE-A.jar"/>  
    36.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.core-3.0.1.RELEASE-A.jar"/>  
    37.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.expression-3.0.1.RELEASE-A.jar"/>  
    38.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.instrument-3.0.1.RELEASE-A.jar"/>  
    39.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.instrument.tomcat-3.0.1.RELEASE-A.jar"/>  
    40.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.jdbc-3.0.1.RELEASE-A.jar"/>  
    41.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.orm-3.0.1.RELEASE-A.jar"/>  
    42.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.oxm-3.0.1.RELEASE-A.jar"/>  
    43.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.transaction-3.0.1.RELEASE-A.jar"/>  
    44.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.web-3.0.1.RELEASE-A.jar"/>  
    45.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.web.portlet-3.0.1.RELEASE-A.jar"/>  
    46.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.web.servlet-3.0.1.RELEASE-A.jar"/>  
    47.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/org.springframework.web.struts-3.0.1.RELEASE-A.jar"/>  
    48.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/persistence.jar"/>  
    49.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-3.3.jar"/>  
    50.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-commons-3.3.jar"/>  
    51.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-tree-3.3.jar"/>  
    52.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-fileupload-1.2.2.jar"/>  
    53.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-io-2.0.1.jar"/>  
    54.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-lang3-3.1.jar"/>  
    55.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/freemarker-2.3.19.jar"/>  
    56.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/javassist-3.11.0.GA.jar"/>  
    57.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/ognl-3.0.5.jar"/>  
    58.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts2-core-2.3.4.jar" sourcepath="F:/Java/SSH Source/struts-2.3.4/src/core/src/main"/>  
    59.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/xwork-core-2.3.4.jar"/>  
    60.     <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java-5.1.7-bin.jar"/>  
    61.     <classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>  
    62. </classpath>  
    1. package org.zbq.bean;  
    2.   
    3. public class User {  
    4.     private int id;  
    5.     private String firstName;  
    6.     private String lastName;  
    7.     private int age;  
    8.     public int getId() {  
    9.         return id;  
    10.     }  
    11.     public void setId(int id) {  
    12.         this.id = id;  
    13.     }  
    14.     public String getFirstName() {  
    15.         return firstName;  
    16.     }  
    17.     public void setFirstName(String firstName) {  
    18.         this.firstName = firstName;  
    19.     }  
    20.     public String getLastName() {  
    21.         return lastName;  
    22.     }  
    23.     public void setLastName(String lastName) {  
    24.         this.lastName = lastName;  
    25.     }  
    26.     public int getAge() {  
    27.         return age;  
    28.     }  
    29.     public void setAge(int age) {  
    30.         this.age = age;  
    31.     }  
    32.     @Override  
    33.     public String toString() {  
    34.         return "ID: " + id + "FirstName: " + firstName;  
    35.     }  
    36. }  

    User.hbm.xml

    1. <?xml version='1.0' encoding='UTF-8'?>  
    2. <!DOCTYPE hibernate-mapping PUBLIC  
    3.           "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
    5. <hibernate-mapping>  
    6.     <class name="org.zbq.bean.User" table="User">  
    7.         <id name="id" type="int">  
    8.             <generator class="increment"></generator>  
    9.         </id>  
    10.           
    11.         <property name="firstName" column="firstName" type="string"></property>  
    12.         <property name="lastName" column="lastName" type="string"></property>  
    13.         <property name="age" column="age" type="int"></property>  
    14.           
    15.     </class>  
    16. </hibernate-mapping>  

    UserDAO.java (Interface)

    1. package org.zbq.dao;  
    2.   
    3. import java.util.List;  
    4.   
    5. import org.zbq.bean.User;  
    6.   
    7. public interface UserDAO {  
    8.     public void doCreateUser(User user);  
    9.     public List<User> findAllUsers();  
    10.     public void removeUser(User user);  
    11.     public void updateUser(User user);  
    12.     public User findUserById(int id);  
    13. }  

    UserDAOImpl.java

    1. package org.zbq.dao.impl;  
    2.   
    3. import java.util.List;  
    4.   
    5. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
    6. import org.zbq.bean.User;  
    7. import org.zbq.dao.UserDAO;  
    8.   
    9. public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {  
    10.   
    11.     public void doCreateUser(User user) {  
    12.         this.getHibernateTemplate().save(user);  
    13.     }  
    14.   
    15.     @SuppressWarnings("unchecked")  
    16.     public List<User> findAllUsers() {  
    17.         String hql = "from User u order by u.id asc";  
    18.         List<User> list = this.getHibernateTemplate().find(hql);  
    19.         return list;  
    20.     }  
    21.   
    22.     public void removeUser(User user) {  
    23.         this.getHibernateTemplate().delete(user);  
    24.     }  
    25.   
    26.     public void updateUser(User user) {  
    27.         this.getHibernateTemplate().update(user);  
    28.     }  
    29.   
    30.     public User findUserById(int id) {  
    31.         User user = null;  
    32.         user = (User) this.getHibernateTemplate().get(User.class, id);  
    33.         return user;  
    34.     }  
    35.       
    36. }  

    UserService.java (Interface)

    1. package org.zbq.service;  
    2.   
    3. import java.util.List;  
    4.   
    5. import org.zbq.bean.User;  
    6.   
    7. public interface UserService {  
    8.     public void doCreateUser(User user);  
    9.     public List<User> findAllUsers();  
    10.     public void delete(User user);  
    11.     public void update(User user);  
    12.     public User findUserById(int id);  
    13. }  

    UserServiceImpl

    1. package org.zbq.service.impl;  
    2.   
    3. import java.util.List;  
    4.   
    5. import org.zbq.bean.User;  
    6. import org.zbq.dao.UserDAO;  
    7. import org.zbq.service.UserService;  
    8.   
    9. public class UserServiceImpl implements UserService {  
    10.     private UserDAO dao;  
    11.       
    12.     public UserDAO getDao() {  
    13.         return dao;  
    14.     }  
    15.   
    16.     public void setDao(UserDAO dao) {  
    17.         this.dao = dao;  
    18.     }  
    19.   
    20.     public void doCreateUser(User user) {  
    21.         this.dao.doCreateUser(user);  
    22.     }  
    23.   
    24.     public List<User> findAllUsers() {  
    25.         return this.dao.findAllUsers();  
    26.     }  
    27.   
    28.     public void delete(User user) {  
    29.         this.dao.removeUser(user);  
    30.     }  
    31.   
    32.     public void update(User user) {  
    33.         this.dao.updateUser(user);  
    34.     }  
    35.   
    36.     public User findUserById(int id) {  
    37.         return this.dao.findUserById(id);  
    38.     }  
    39.   
    40. }  

    关于User的Action

    SaveUserAction.java

    1. package org.zbq.action.user;  
    2.   
    3. import org.zbq.bean.User;  
    4. import org.zbq.service.UserService;  
    5.   
    6. import com.opensymphony.xwork2.ActionSupport;  
    7.   
    8. public class SaveUserAction extends ActionSupport {  
    9.     private User user;  
    10.     private UserService userService;  
    11.       
    12.     public UserService getUserService() {  
    13.         return userService;  
    14.     }  
    15.   
    16.     public void setUserService(UserService userService) {  
    17.         this.userService = userService;  
    18.     }  
    19.   
    20.     public User getUser() {  
    21.         return user;  
    22.     }  
    23.   
    24.     public void setUser(User user) {  
    25.         this.user = user;  
    26.     }  
    27.   
    28.     @Override  
    29.     public String execute() throws Exception {  
    30. //      调用service相关的方法,完成实际的业务处理  
    31.         this.userService.doCreateUser(user);  
    32.         return SUCCESS ;  
    33.     }  
    34. }  

    ListUserAction.java

    1. package org.zbq.action.user;  
    2.   
    3. import java.util.Map;  
    4.   
    5. import org.zbq.service.UserService;  
    6.   
    7. import com.opensymphony.xwork2.ActionContext;  
    8. import com.opensymphony.xwork2.ActionSupport;  
    9.   
    10. public class ListUserAction extends ActionSupport {  
    11.     private UserService userService;  
    12.   
    13.     public UserService getUserService() {  
    14.         return userService;  
    15.     }  
    16.   
    17.     public void setUserService(UserService userService) {  
    18.         this.userService = userService;  
    19.     }  
    20.       
    21.     @SuppressWarnings("unchecked")  
    22.     @Override  
    23.     public String execute() throws Exception {  
    24.           
    25.         Map request = (Map) ActionContext.getContext().get("request");  
    26.           
    27.         request.put("list", this.userService.findAllUsers());  
    28.         return SUCCESS;  
    29.     }  
    30. }  

    DeleteUserAction.java

    1. package org.zbq.action.user;  
    2.   
    3. import org.zbq.bean.User;  
    4. import org.zbq.service.UserService;  
    5.   
    6. import com.opensymphony.xwork2.ActionSupport;  
    7.   
    8. public class DeleteUserAction extends ActionSupport {  
    9.     private User user;  
    10.     private UserService userService;  
    11.       
    12.     public User getUser() {  
    13.         return user;  
    14.     }  
    15.   
    16.     public void setUser(User user) {  
    17.         this.user = user;  
    18.     }  
    19.   
    20.     public UserService getUserService() {  
    21.         return userService;  
    22.     }  
    23.   
    24.     public void setUserService(UserService userService) {  
    25.         this.userService = userService;  
    26.     }  
    27.   
    28.     @Override  
    29.     public String execute() throws Exception {  
    30.         this.userService.delete(user);  
    31.         return SUCCESS;  
    32.     }  
    33. }  

    UpdatePUser.java

    1. package org.zbq.action.user;  
    2.   
    3. import org.zbq.bean.User;  
    4. import org.zbq.service.UserService;  
    5.   
    6. import com.opensymphony.xwork2.ActionSupport;  
    7.   
    8. public class UpdatePUser extends ActionSupport {  
    9.     private User user;  
    10.     private UserService userService;  
    11.       
    12.     public User getUser() {  
    13.         return user;  
    14.     }  
    15.   
    16.     public void setUser(User user) {  
    17.         this.user = user;  
    18.     }  
    19.   
    20.     public UserService getUserService() {  
    21.         return userService;  
    22.     }  
    23.   
    24.     public void setUserService(UserService userService) {  
    25.         this.userService = userService;  
    26.     }  
    27.   
    28.     @Override  
    29.     public String execute() throws Exception {  
    30.         this.user = this.userService.findUserById(user.getId());  
    31.           
    32.         return SUCCESS;  
    33.     }  
    34. }  

    UpdateUserAction.java

    1. package org.zbq.action.user;  
    2.   
    3. import org.zbq.bean.User;  
    4. import org.zbq.service.UserService;  
    5.   
    6. import com.opensymphony.xwork2.ActionSupport;  
    7.   
    8. public class UpdateUserAction extends ActionSupport {  
    9.     private User user;  
    10.     private UserService userService;  
    11.     public User getUser() {  
    12.         return user;  
    13.     }  
    14.     public void setUser(User user) {  
    15.         this.user = user;  
    16.     }  
    17.     public UserService getUserService() {  
    18.         return userService;  
    19.     }  
    20.     public void setUserService(UserService userService) {  
    21.         this.userService = userService;  
    22.     }  
    23.     @Override  
    24.     public String execute() throws Exception {  
    25.         this.userService.update(this.user);  
    26.         return SUCCESS;  
    27.     }  
    28. }  

    struts.xml

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE struts PUBLIC  
    3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
    4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
    5.   
    6. <struts>  
    7.     <package name="ssh2" extends="struts-default">  
    8.         <action name="saveUser" class="saveUserAction">  
    9.             <result name="success" type="redirect">listUser.action</result>  
    10.         </action>  
    11.           
    12.         <action name="listUser" class="listUserAction">  
    13.             <result name="success">/list.jsp</result>  
    14.         </action>  
    15.           
    16.         <action name="deleteUser" class="removeUserAction">  
    17.             <result name="success" type="redirect">listUser.action</result>  
    18.         </action>  
    19.           
    20.         <action name="updatePUser" class="updatePUserAction">  
    21.             <result name="success">/update.jsp</result>  
    22.         </action>  
    23.           
    24.         <action name="updateUser" class="updateUserAction">  
    25.             <result name="success" type="redirect">listUser.action</result>  
    26.         </action>  
    27.     </package>  
    28. </struts>  

    web.xml

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="2.5"   
    3.     xmlns="http://java.sun.com/xml/ns/javaee"   
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    7.   <welcome-file-list>  
    8.     <welcome-file>index.jsp</welcome-file>  
    9.   </welcome-file-list>  
    10.     
    11.   <filter>  
    12.     <filter-name>struts2</filter-name>  
    13.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
    14.   </filter>  
    15.     
    16.   <filter-mapping>  
    17.     <filter-name>struts2</filter-name>  
    18.     <url-pattern>/*</url-pattern>  
    19.   </filter-mapping>  
    20.     
    21.   <listener>  
    22.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    23.   </listener>  
    24.     
    25. </web-app>  

    applictionContext.xml

    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans  
    3.     xmlns="http://www.springframework.org/schema/beans"  
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.     xmlns:p="http://www.springframework.org/schema/p"  
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
    7.       
    8.     <!-- 想到与Hibernate的hibernate.cfg.xml -->  
    9.     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    10.         <property name="driverClassName">  
    11.             <value>org.gjt.mm.mysql.Driver</value>  
    12.         </property>  
    13.           
    14.         <property name="url">  
    15.             <value>jdbc:mysql://127.0.0.1:3306/ssh</value>  
    16.         </property>  
    17.           
    18.         <property name="username">  
    19.             <value></value>  
    20.         </property>  
    21.           
    22.         <property name="password">  
    23.             <value></value>  
    24.         </property>  
    25.           
    26.     </bean>  
    27.   
    28.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
    29.         <property name="dataSource">  
    30.             <ref local="dataSource"/>  
    31.         </property>  
    32.           
    33.         <property name="mappingResources">  
    34.             <list>  
    35.                 <value>org/zbq/bean/User.hbm.xml</value>                      
    36.             </list>  
    37.         </property>  
    38.           
    39.         <property name="hibernateProperties">  
    40.             <props>  
    41.                 <prop key="hibernate.dialect">  
    42.                     org.hibernate.dialect.MySQLDialect  
    43.                 </prop>  
    44.                   
    45.                 <prop key="hibernate.show_sql">  
    46.                     true  
    47.                 </prop>  
    48.             </props>  
    49.         </property>  
    50.           
    51.     </bean>  
    52.       
    53.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
    54.         <property name="sessionFactory">  
    55.             <ref local="sessionFactory"/>  
    56.         </property>  
    57.     </bean>  
    58.       
    59.     <bean id="userDAO" class="org.zbq.dao.impl.UserDAOImpl" scope="singleton">  
    60.         <property name="sessionFactory">  
    61.             <ref local="sessionFactory"/>  
    62.         </property>  
    63.     </bean>  
    64.       
    65.     <bean id="userServiceTarget" class="org.zbq.service.impl.UserServiceImpl" scope="singleton">  
    66.         <property name="dao" ref="userDAO" />  
    67.     </bean>  
    68.       
    69.     <bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
    70.         <property name="target">  
    71.             <ref local="userServiceTarget"/>  
    72.         </property>  
    73.           
    74.         <property name="transactionManager">  
    75.             <ref local="transactionManager"/>  
    76.         </property>  
    77.           
    78.         <property name="transactionAttributes">  
    79.             <props>  
    80.                 <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>  
    81.                 <prop key="*">PROPAGATION_REQUIRED</prop>  
    82.             </props>  
    83.         </property>  
    84.     </bean>  
    85.       
    86.     <bean id="saveUserAction" class="org.zbq.action.user.SaveUserAction" scope="prototype">  
    87.         <property name="userService" ref="userService"></property>  
    88.     </bean>  
    89.       
    90.     <bean id="listUserAction" class="org.zbq.action.user.ListUserAction" scope="prototype">  
    91.         <property name="userService" ref="userService"></property>  
    92.     </bean>  
    93.       
    94.     <bean id="removeUserAction" class="org.zbq.action.user.DeleteUserAction" scope="prototype">  
    95.         <property name="userService" ref="userService"></property>        
    96.     </bean>  
    97.       
    98.     <bean id="updatePUserAction" class="org.zbq.action.user.UpdatePUser">  
    99.         <property name="userService" ref="userService"></property>  
    100.     </bean>  
    101.       
    102.     <bean id="updateUserAction" class="org.zbq.action.user.UpdateUserAction" scope="prototype">  
    103.         <property name="userService" ref="userService"></property>  
    104.     </bean>  
    105. </beans>  



    index.jsp

    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
    2. <%@ taglib prefix="s" uri="/struts-tags" %>  
    3.   
    4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    5. <html>  
    6.   <head>  
    7.       
    8.     <title>My JSP 'index.jsp' starting page</title>  
    9.   
    10.   </head>  
    11.     
    12.   <body>  
    13.     <h1><font color="red">Operation List</font></h1>  
    14.     <s:a href="save.jsp">Save User</s:a><br/>  
    15.     <s:a href="listUser.action">List Users</s:a>  
    16.       
    17.       
    18.   </body>  
    19. </html>  

    list.jsp

    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
    2. <%@ taglib prefix="s" uri="/struts-tags"%>  
    3.   
    4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    5. <html>  
    6.   <head>  
    7.     <base href="<%=basePath%>">  
    8.       
    9.     <title>My JSP 'list.jsp' starting page</title>  
    10.     <script type="text/javascript">  
    11.         function del(){  
    12.             if(confirm("Are you sure?")){  
    13.                 return true;  
    14.             }  
    15.             return false;  
    16.         }  
    17.     </script>  
    18.   </head>  
    19.     
    20.   <body>  
    21.     <h1><font color="red">Users List</font></h1>  
    22.     <s:a href="/ssh2/index.jsp"><<--</s:a>  
    23.     <table border="1" width="80%" align="center">  
    24.         <tr>  
    25.             <th>序号</th>  
    26.             <th>姓</th>  
    27.             <th>名</th>  
    28.             <th>年龄</th>  
    29.             <th>删除</th>  
    30.             <th>更新</th>  
    31.         </tr>  
    32.           
    33.         <s:iterator value="#request.list" id="us">  
    34.             <tr>  
    35.                 <td>  
    36.                     <s:property value="#us.id"/>  
    37.                 </td>  
    38.                 <td>  
    39.                     <s:property value="#us.firstName"/>  
    40.                 </td>  
    41.                 <td>  
    42.                     <s:property value="#us.lastName"/>  
    43.                 </td>  
    44.                 <td>  
    45.                     <s:property value="#us.age"/>  
    46.                 </td>  
    47.                 <td align="center">  
    48.                     <s:a href="deleteUser.action?user.id=%{#us.id}" onclick="return del()">Delete</s:a>  
    49.                 </td>  
    50.                 <td align="center">  
    51.                     <s:a href="updatePUser.action?user.id=%{#us.id}">Update</s:a>         
    52.                 </td>  
    53.             </tr>  
    54.         </s:iterator>  
    55.     </table>  
    56.       
    57.   </body>  
    58. </html>  

    save.jsp

    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
    2. <%@ taglib prefix="s" uri="/struts-tags" %>  
    3.   
    4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    5. <html>  
    6.   <head>  
    7.       
    8.     <title>My JSP 'index.jsp' starting page</title>  
    9.   
    10.   </head>  
    11.     
    12.   <body>  
    13.     <h1><font color="red">Save User</font></h1>  
    14.           
    15.     <s:form action="saveUser">  
    16.         <s:textfield name="user.firstName" label="First Name"></s:textfield>  
    17.         <s:textfield name="user.lastName" label="Last Name"></s:textfield>  
    18.         <s:textfield name="user.age" label="Age"></s:textfield>  
    19.         <s:submit></s:submit>  
    20.     </s:form>  
    21.       
    22.   </body>  
    23. </html>  

    update.jsp

      1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
      2. <%@ taglib prefix="s" uri="/struts-tags"%>  
      3.   
      4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
      5. <html>  
      6.   <head>  
      7.     <base href="<%=basePath%>">  
      8.       
      9.     <title>My JSP 'update.jsp' starting page</title>  
      10.   
      11.   </head>  
      12.     
      13.   <body>  
      14.     <h1><font color="red">Update User</font></h1>  
      15.       
      16.     <s:form action="updateUser">  
      17.     <table>  
      18.         <tr>  
      19.             <td>  
      20.                 <s:hidden name="user.id" value="%{user.id}"></s:hidden>  
      21.             </td>  
      22.         </tr>  
      23.       
      24.         <tr>  
      25.             <td>  
      26.                 <s:textfield name="user.firstName" value="%{user.firstName}" label="First Name"></s:textfield>  
      27.             </td>  
      28.         </tr>  
      29.           
      30.         <tr>  
      31.             <td>  
      32.                 <s:textfield name="user.lastName" value="%{user.lastName}" label="Last Name"></s:textfield>  
      33.             </td>  
      34.         </tr>  
      35.           
      36.         <tr>  
      37.             <td>  
      38.                 <s:textfield name="user.age" value="%{user.age}" label="Age"></s:textfield>  
      39.             </td>  
      40.         </tr>  
      41.           
      42.         <tr>  
      43.             <td>  
      44.                 <s:submit></s:submit>  
      45.             </td>  
      46.         </tr>  
      47.     </table>    
      48.     </s:form>  
      49.       
      50.   </body>  
      51. </html
  • 相关阅读:
    Servlet3.0之五:servlet3.0下的web.xml
    RESTLET开发实例(二)使用Component、Application的REST服务
    并发容器之ConcurrentLinkedDeque
    script的onerror事件支持情况调查
    纯CSS后台框架
    浏览器对width与height的最大值限制
    DOM系统中的各种队列
    跨域的异步请求四
    JSON parse在各浏览器的兼容性列表
    在IE6~8下创建HTML5新标签
  • 原文地址:https://www.cnblogs.com/ZhuRenWang/p/4800246.html
Copyright © 2011-2022 走看看