zoukankan      html  css  js  c++  java
  • SSH项目整合基本步骤

    SSH项目整合基本步骤

    一、项目简介

      该项目是由Spring4、Struts2 以及 Hibernate4 整合搭建的 web 项目,把Action分开编写,便于查看,使用JSTL、EL标签。

    二、项目目录

    三、项目所需Jar包

      ①Spring:

      aopalliance-1.0.jar
      aspectjweaver.jar
      spring-aop-4.3.0.RELEASE.jar
      spring-aspects-4.3.0.RELEASE.jar
      spring-beans-4.3.0.RELEASE.jar
      spring-context-4.3.0.RELEASE.jar
      spring-core-4.3.0.RELEASE.jar
      spring-expression-4.3.0.RELEASE.jar
      spring-jdbc-4.3.0.RELEASE.jar
      spring-orm-4.3.0.RELEASE.jar
      spring-tx-4.3.0.RELEASE.jar
      spring-web-4.3.0.RELEASE.jar

      ②Struts2:

      asm-5.0.2.jar
      asm-commons-5.0.2.jar
      asm-tree-5.0.2.jar
      commons-fileupload-1.3.1.jar
      commons-io-2.2.jar
      commons-lang3-3.2.jar
      freemarker-2.3.19.jar
      ognl-3.0.6.jar
      struts2-core-2.3.20.jar
      struts2-spring-plugin-2.3.20.jar
      xwork-core-2.3.20.jar

      ③Hibernate:

      antlr-2.7.7.jar
      dom4j-1.6.1.jar
      hibernate-commons-annotations-4.0.5.Final.jar
      hibernate-core-4.3.11.Final.jar
      hibernate-jpa-2.1-api-1.0.0.Final.jar
      jandex-1.1.0.Final.jar
      jboss-logging-3.1.3.GA.jar
      jboss-logging-annotations-1.2.0.Beta1.jar
      jboss-transaction-api_1.2_spec-1.0.0.Final.jar

      ④C3P0:

      c3p0-0.9.5.1.jar
      mchange-commons-java-0.2.10.jar
      mysql-connector-java-5.1.8-bin.jar

      ⑤JSTL:

      taglibs-standard-compat-1.2.5.jar
      taglibs-standard-impl-1.2.5.jar
      taglibs-standard-jstlel-1.2.5.jar
      taglibs-standard-spec-1.2.5.jar

      ⑥log4j:

      log4j-1.2.17.jar

    四、项目源码

    1、db.properties

    1 db.username=root
    2 db.password=root
    3 db.driverClass=com.mysql.jdbc.Driver
    4 db.jdbcUrl=jdbc:mysql://localhost:3306/ssh_01
    5 db.initialPoolSize=2
    6 db.maxPoolSize=20

    2、log4j.properties

     1 # Global logging configuration
     2 log4j.rootLogger=ERROR, stdout
     3 # MyBatis logging configuration...
     4 log4j.logger.org.mybatis.example.BlogMapper=TRACE
     5 # Console output...
     6 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
     7 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
     8 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
     9 # Start logging
    10 log4j.logger.cn.com.zfc.model.UserMapper=TRACE

    3、applicationContext.xml(可以把通用的配置抽取出来,便于管理)

      1 <?xml version="1.0" encoding="UTF-8"?>
      2 <beans xmlns="http://www.springframework.org/schema/beans"
      3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
      4     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
      5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      6         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
      8         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
      9 
     10     <!-- 注册数据库链接文件 -->
     11     <context:property-placeholder location="classpath:db.properties" />
     12 
     13     <!-- 注册C3P0 数据源 -->
     14     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     15         <property name="user" value="${db.username}"></property>
     16         <property name="password" value="${db.password}"></property>
     17         <property name="driverClass" value="${db.driverClass}"></property>
     18         <property name="jdbcUrl" value="${db.jdbcUrl}"></property>
     19         <property name="initialPoolSize" value="${db.initialPoolSize}"></property>
     20         <property name="maxPoolSize" value="${db.maxPoolSize}"></property>
     21     </bean>
     22 
     23     <!-- 注册 LocalSessionFactoryBean,这种配置可以省略 Hibernate 的主配置文件 -->
     24     <bean id="sessionFactory"
     25         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
     26         <property name="dataSource" ref="dataSource"></property>
     27         <property name="hibernateProperties">
     28             <props>
     29                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
     30                 <prop key="hibernate.show_sql">true</prop>
     31                 <prop key="hibernate.format_sql">true</prop>
     32                 <prop key="hibernate.hbm2ddl.auto">update</prop>
     33             </props>
     34         </property>
     35         <!-- 配置 hibernate 映射文件的位置及名称,可以直接写目录 -->
     36         <property name="mappingDirectoryLocations">
     37             <list>
     38                 <value>classpath:cn/com/zfc/ssh/entity</value>
     39             </list>
     40         </property>
     41     </bean>
     42 
     43     <!-- 注册事务管理器 -->
     44     <bean id="transactionManager"
     45         class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     46         <property name="sessionFactory" ref="sessionFactory"></property>
     47     </bean>
     48 
     49     <!-- 注册通知,是对 Service 层的方法起作用 -->
     50     <tx:advice transaction-manager="transactionManager" id="txadvice">
     51         <tx:attributes>
     52             <tx:method name="add*" propagation="REQUIRED" />
     53             <tx:method name="remove*" propagation="REQUIRED" />
     54             <tx:method name="modify*" propagation="REQUIRED" />
     55             <tx:method name="find*" read-only="true" />
     56             <tx:method name="*" read-only="true" />
     57         </tx:attributes>
     58     </tx:advice>
     59 
     60     <!-- 注册切面 -->
     61     <aop:config>
     62         <!-- Service 层 -->
     63         <aop:pointcut expression="execution(* *..service.*.*(..))"
     64             id="txPoint" />
     65         <aop:advisor advice-ref="txadvice" pointcut-ref="txPoint" />
     66     </aop:config>
     67 
     68     <!-- 注册 User -->
     69     <!-- Dao -->
     70     <bean id="userDao" class="cn.com.zfc.ssh.dao.impl.UserDaoImpl">
     71         <property name="sessionFactory" ref="sessionFactory"></property>
     72     </bean>
     73 
     74     <!-- Service -->
     75     <bean id="userService" class="cn.com.zfc.ssh.service.impl.UserServiceImpl">
     76         <property name="userDao" ref="userDao"></property>
     77     </bean>
     78 
     79     <!-- Action -->
     80     <!-- 跳转到 User 添加的视图的Action -->
     81     <bean id="userAddViewAction" class="cn.com.zfc.ssh.action.UserAddViewAction"></bean>
     82 
     83     <!-- User 添加的 Action -->
     84     <bean id="userAddAction" class="cn.com.zfc.ssh.action.UserAddAction">
     85         <property name="userService" ref="userService"></property>
     86     </bean>
     87 
     88     <!-- User 查询全部的 Action -->
     89     <bean id="usersFindAction" class="cn.com.zfc.ssh.action.UsersFindAction">
     90         <property name="userService" ref="userService"></property>
     91     </bean>
     92 
     93     <!-- User 删除的 Action -->
     94     <bean id="userRemoveAction" class="cn.com.zfc.ssh.action.UserRemoveAction">
     95         <property name="userService" ref="userService"></property>
     96     </bean>
     97 
     98 
     99     <!-- 跳转到 User 修改视图的 Action -->
    100     <bean id="userModifyViewAction" class="cn.com.zfc.ssh.action.UserModifyViewAction">
    101         <property name="userService" ref="userService"></property>
    102     </bean>
    103 
    104     <!-- User 修改的 Action -->
    105     <bean id="userModifyAction" class="cn.com.zfc.ssh.action.UserModifyAction">
    106         <property name="userService" ref="userService"></property>
    107     </bean>
    108 
    109 
    110 </beans>

    4、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     <!-- 声明对象是由 Spring 创建的 -->
     8     <constant name="struts.objectFactory" value="spring"></constant>
     9     
    10     <!-- 定义包,有关 User 的配置 -->
    11     <package name="userPackage" extends="struts-default" namespace="/">
    12         <!-- User 添加 -->
    13         <action name="userAddViewAction" class="userAddViewAction"
    14             method="userAddView">
    15             <result>/user_add.jsp</result>
    16         </action>
    17 
    18         <!-- 添加页面 -->
    19         <action name="userAddAction" class="userAddAction" method="addUser">
    20             <!-- 直接跳转到查询全部 User 的Action -->
    21             <result type="redirectAction">/usersFindAction</result>
    22         </action>
    23 
    24         <!-- Users 查询 -->
    25         <action name="usersFindAction" class="usersFindAction" method="findUsers">
    26             <result>/users_query.jsp</result>
    27         </action>
    28 
    29         <!-- User 删除 -->
    30         <action name="userRemoveAction" class="userRemoveAction"
    31             method="removeUser">
    32             <result type="redirectAction">/usersFindAction</result>
    33         </action>
    34         <!-- User 修改页面 -->
    35         <action name="userModifyViewAction" class="userModifyViewAction"
    36             method="modifyUserView">
    37             <result>/user-modify.jsp</result>
    38         </action>
    39 
    40         <!-- User 修改 -->
    41         <action name="userModifyAction" class="userModifyAction"
    42             method="modifyUser">
    43             <result type="redirectAction">/usersFindAction</result>
    44         </action>
    45 
    46     </package>
    47 </struts>

    5、web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
     3   <display-name>SSH</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.jsp</welcome-file>
     6   </welcome-file-list>
     7   <context-param>
     8     <param-name>contextConfigLocation</param-name>
     9     <param-value>classpath:applicationContext.xml</param-value>
    10   </context-param>
    11   <listener>
    12     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    13   </listener>
    14   <filter>
    15     <filter-name>encodingFilter</filter-name>
    16     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    17     <async-supported>true</async-supported>
    18     <init-param>
    19       <param-name>encoding</param-name>
    20       <param-value>UTF-8</param-value>
    21     </init-param>
    22   </filter>
    23   <filter-mapping>
    24     <filter-name>encodingFilter</filter-name>
    25     <url-pattern>/*</url-pattern>
    26   </filter-mapping>
    27   <filter>
    28     <filter-name>struts2</filter-name>
    29     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    30   </filter>
    31   <filter-mapping>
    32     <filter-name>struts2</filter-name>
    33     <url-pattern>/*</url-pattern>
    34   </filter-mapping>
    35 </web-app>

    6、User.java

     1 package cn.com.zfc.ssh.entity;
     2 
     3 public class User {
     4     private Integer id;
     5     private String userName;
     6     private int userAge;
     7 
     8     public User() {
     9         super();
    10     }
    11 
    12     public Integer getId() {
    13         return id;
    14     }
    15 
    16     public void setId(Integer id) {
    17         this.id = id;
    18     }
    19 
    20     public String getUserName() {
    21         return userName;
    22     }
    23 
    24     public void setUserName(String userName) {
    25         this.userName = userName;
    26     }
    27 
    28     public int getUserAge() {
    29         return userAge;
    30     }
    31 
    32     public void setUserAge(int userAge) {
    33         this.userAge = userAge;
    34     }
    35 
    36     @Override
    37     public String toString() {
    38         return "User [id=" + id + ", userName=" + userName + ", userAge=" + userAge + "]";
    39     }
    40 
    41 }

    7、User.hbm.xml

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     4 <!-- Generated 2017-6-9 23:41:28 by Hibernate Tools 3.5.0.Final -->
     5 <hibernate-mapping package="cn.com.zfc.ssh.entity">
     6     <class name="User" table="USER_INFO">
     7         <id name="id" type="java.lang.Integer">
     8             <column name="ID" />
     9             <!-- 主键生成策略 -->
    10             <generator class="native" />
    11         </id>
    12         <property name="userName" type="java.lang.String">
    13             <column name="USER_NAME" />
    14         </property>
    15         <property name="userAge" type="int">
    16             <column name="USER_AGE" />
    17         </property>
    18     </class>
    19 </hibernate-mapping>

    8、UserDao.java

     1 package cn.com.zfc.ssh.dao;
     2 
     3 import java.util.List;
     4 
     5 import cn.com.zfc.ssh.entity.User;
     6 
     7 public interface UserDao {
     8     void insertUser(User user);
     9 
    10     void deleteUser(int id);
    11 
    12     void updateUser(User user);
    13     
    14     User queryUserById(int id);
    15 
    16     List<User> queryUsers();
    17 }

    9、UserDaoImpl.java

     1 package cn.com.zfc.ssh.dao.impl;
     2 
     3 import java.util.List;
     4 
     5 import org.hibernate.SessionFactory;
     6 
     7 import cn.com.zfc.ssh.dao.UserDao;
     8 import cn.com.zfc.ssh.entity.User;
     9 /**
    10  * 
    11  * @title UserDaoImpl
    12  * @describe User 相关的 Dao 层操作,Session 要与当前线程绑定
    13  * @author zfc
    14  * @date 2017年6月10日下午3:31:49
    15  */
    16 public class UserDaoImpl implements UserDao {
    17 
    18     private SessionFactory sessionFactory;
    19 
    20     public void setSessionFactory(SessionFactory sessionFactory) {
    21         this.sessionFactory = sessionFactory;
    22     }
    23 
    24     @Override
    25     public void insertUser(User user) {
    26         sessionFactory.getCurrentSession().save(user);
    27     }
    28 
    29     @Override
    30     public void deleteUser(int id) {
    31         User user = (User) sessionFactory.getCurrentSession().get(User.class, id);
    32         sessionFactory.getCurrentSession().delete(user);
    33 
    34     }
    35 
    36     @Override
    37     public void updateUser(User user) {
    38         sessionFactory.getCurrentSession().update(user);
    39 
    40     }
    41 
    42     @Override
    43     public List<User> queryUsers() {
    44         String hql = "From User";
    45         return sessionFactory.getCurrentSession().createQuery(hql).list();
    46     }
    47 
    48     @Override
    49     public User queryUserById(int id) {
    50         return (User) sessionFactory.getCurrentSession().get(User.class, id);
    51     }
    52 }

    10、UserService.java

     1 package cn.com.zfc.ssh.service;
     2 
     3 import java.util.List;
     4 
     5 import cn.com.zfc.ssh.entity.User;
     6 
     7 public interface UserService {
     8     void addUser(User user);
     9 
    10     void removeUser(int id);
    11 
    12     void modifyUser(User user);
    13 
    14     List<User> findUsers();
    15 
    16     User findUserById(int id);
    17 }

    11、UserServiceImpl.java

     1 package cn.com.zfc.ssh.service.impl;
     2 
     3 import java.util.List;
     4 
     5 import cn.com.zfc.ssh.dao.UserDao;
     6 import cn.com.zfc.ssh.entity.User;
     7 import cn.com.zfc.ssh.service.UserService;
     8 /**
     9  * 
    10  * @title UserServiceImpl
    11  * @describe 业务逻辑实现
    12  * @author zfc
    13  * @date 2017年6月11日下午8:16:50
    14  */
    15 public class UserServiceImpl implements UserService {
    16 
    17     private UserDao userDao;
    18 
    19     public void setUserDao(UserDao userDao) {
    20         this.userDao = userDao;
    21     }
    22 
    23     @Override
    24     public void addUser(User user) {
    25         userDao.insertUser(user);
    26 
    27     }
    28 
    29     @Override
    30     public void removeUser(int id) {
    31         userDao.deleteUser(id);
    32 
    33     }
    34 
    35     @Override
    36     public void modifyUser(User user) {
    37         userDao.updateUser(user);
    38 
    39     }
    40 
    41     @Override
    42     public List<User> findUsers() {
    43         return userDao.queryUsers();
    44     }
    45 
    46     @Override
    47     public User findUserById(int id) {
    48         return userDao.queryUserById(id);
    49     }
    50 
    51 }

    12、UserAddViewAction.java

     1 package cn.com.zfc.ssh.action;
     2 /**
     3  * 
     4  * @title UserAddViewAction
     5  * @describe User 添加的视图的 Action
     6  * @author zfc
     7  * @date 2017年6月10日下午3:29:15
     8  */
     9 public class UserAddViewAction {
    10 
    11     public String userAddView() {
    12         return "success";
    13     }
    14 }

    13、UserAddAction.java

     1 package cn.com.zfc.ssh.action;
     2 
     3 import cn.com.zfc.ssh.entity.User;
     4 import cn.com.zfc.ssh.service.UserService;
     5 /**
     6  * 
     7  * @title UserAddAction
     8  * @describe User 添加的Action
     9  * @author zfc
    10  * @date 2017年6月10日下午3:28:57
    11  */
    12 public class UserAddAction {
    13 
    14     private UserService userService;
    15     private User user;
    16 
    17     public User getUser() {
    18         return user;
    19     }
    20 
    21     public void setUser(User user) {
    22         this.user = user;
    23     }
    24 
    25     public void setUserService(UserService userService) {
    26         this.userService = userService;
    27     }
    28 
    29     public String addUser() {
    30         // System.out.println("user:" + user);
    31         userService.addUser(user);
    32         return "success";
    33     }
    34 
    35 }

    14、UsersFindAction.java

     1 package cn.com.zfc.ssh.action;
     2 
     3 import java.util.List;
     4 import java.util.Map;
     5 
     6 import org.apache.struts2.interceptor.RequestAware;
     7 
     8 import cn.com.zfc.ssh.entity.User;
     9 import cn.com.zfc.ssh.service.UserService;
    10 /**
    11  * 
    12  * @title UsersFindAction
    13  * @describe User 查询全部的 Action
    14  * @author zfc
    15  * @date 2017年6月10日下午3:31:07
    16  */
    17 public class UsersFindAction implements RequestAware {
    18 
    19     private UserService userService;
    20     private Map<String, Object> request;
    21 
    22     public void setUserService(UserService userService) {
    23         this.userService = userService;
    24     }
    25 
    26     @Override
    27     public void setRequest(Map<String, Object> request) {
    28         this.request = request;
    29     }
    30 
    31     public String findUsers() {
    32         // System.out.println("查询全部Users Action");
    33         List<User> users = userService.findUsers();
    34         request.put("users", users);
    35         // System.out.println(userService.findUsers());
    36         return "success";
    37     }
    38 
    39 }

    15、UserRemoveAction.java

     1 package cn.com.zfc.ssh.action;
     2 
     3 import cn.com.zfc.ssh.service.UserService;
     4 /**
     5  * 
     6  * @title UserRemoveAction
     7  * @describe User 删除的 Action
     8  * @author zfc
     9  * @date 2017年6月10日下午3:30:50
    10  */
    11 public class UserRemoveAction {
    12     private Integer id;
    13 
    14     private UserService userService;
    15 
    16     public UserService getUserService() {
    17         return userService;
    18     }
    19 
    20     public void setUserService(UserService userService) {
    21         this.userService = userService;
    22     }
    23 
    24     public Integer getId() {
    25         return id;
    26     }
    27 
    28     public void setId(Integer id) {
    29         this.id = id;
    30     }
    31 
    32     public String removeUser() {
    33         userService.removeUser(id);
    34         return "success";
    35     }
    36 }

    16、UserModifyViewAction.java

     1 package cn.com.zfc.ssh.action;
     2 
     3 import java.util.Map;
     4 
     5 import org.apache.struts2.interceptor.RequestAware;
     6 
     7 import cn.com.zfc.ssh.entity.User;
     8 import cn.com.zfc.ssh.service.UserService;
     9 
    10 /**
    11  * 
    12  * @title UserModifyViewAction
    13  * @describe 跳转 User 修改视图的 Action
    14  * @author zfc
    15  * @date 2017年6月10日下午3:29:56
    16  */
    17 public class UserModifyViewAction implements RequestAware {
    18 
    19     private Map<String, Object> request;
    20     private UserService userService;
    21     private Integer id;
    22 
    23     public void setId(Integer id) {
    24         this.id = id;
    25     }
    26 
    27     public void setUserService(UserService userService) {
    28         this.userService = userService;
    29     }
    30 
    31     @Override
    32     public void setRequest(Map<String, Object> request) {
    33         this.request = request;
    34     }
    35 
    36     public String modifyUserView() {
    37         User user = userService.findUserById(id);
    38         // System.out.println(user);
    39         request.put("user", user);
    40         return "success";
    41     }
    42 
    43 }

    17、UserModifyAction.java

     1 package cn.com.zfc.ssh.action;
     2 import cn.com.zfc.ssh.entity.User;
     3 import cn.com.zfc.ssh.service.UserService;
     4 /**
     5  * 
     6  * @title UserModifyAction
     7  * @describe User 修改的Action
     8  * @author zfc
     9  * @date 2017年6月10日下午3:29:40
    10  */
    11 public class UserModifyAction {
    12 
    13     private User user;
    14     private UserService userService;
    15 
    16     public User getUser() {
    17         return user;
    18     }
    19 
    20     public void setUser(User user) {
    21         this.user = user;
    22     }
    23 
    24     public UserService getUserService() {
    25         return userService;
    26     }
    27 
    28     public void setUserService(UserService userService) {
    29         this.userService = userService;
    30     }
    31 
    32     public String modifyUser() {
    33         System.out.println(user);
    34         userService.modifyUser(user);
    35         return "success";
    36     }
    37 
    38 }

    18、index.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>SSH_01</title>
     8 </head>
     9 <body>
    10     <a href="usersFindAction">Users Query</a>
    11     <hr />
    12     <a href="userAddViewAction">User Add</a>
    13 </body>
    14 </html>

    19、user_add.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>用户添加</title>
     9 </head>
    10 <body>
    11     <form action="userAddAction" method="post">
    12         UserName:<input type="text" name="user.userName">
    13         <hr />
    14         UserAge:<input type="text" name="user.userAge">
    15         <hr />
    16         <input type="submit" value="ADD" />
    17     </form>
    18     <hr />
    19     <a href="usersFindAction">Users Query</a>
    20 </body>
    21 </html>

    20、users_query.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>用户查询</title>
     9 </head>
    10 <body>
    11     <table cellspacing="0" cellpadding="0" border="1" width="50%">
    12         <thead>
    13             <tr>
    14                 <th>ID</th>
    15                 <th>UserName</th>
    16                 <th>UserAge</th>
    17                 <th>Operate</th>
    18             </tr>
    19         </thead>
    20         <tbody>
    21             <c:forEach items="${users}" var="user">
    22                 <tr>
    23                     <td>${user.id}</td>
    24                     <td>${user.userName}</td>
    25                     <td>${user.userAge}</td>
    26                     <td><a href="userModifyViewAction?id=${user.id}">Update</a>&amp;<a
    27                         href="userRemoveAction?id=${user.id}">Remove</a></td>
    28                 </tr>
    29             </c:forEach>
    30         </tbody>
    31     </table>
    32     <hr />
    33     <a href="userAddViewAction">User Add</a>
    34 </body>
    35 </html>

    21、user_modify.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11     <form action="userModifyAction" method="post">
    12         ${user}
    13         <!-- 隐藏域,用来保存当前 User 的 id -->
    14         <input type="hidden" name="user.id" value="${user.id}" />
    15         <hr />
    16         UserName:<input type="text" name="user.userName" value="${user.userName}">
    17         <hr />
    18         UserAge:<input type="text" name="user.userAge" value="${user.userAge}">
    19         <hr />
    20         <input type="submit" value="Modify" />
    21     </form>
    22     <hr />
    23     <a href="usersFindAction">Users Query</a>
    24 </body>
    25 </html>

    五、数据库

     六、程序运行效果图

  • 相关阅读:
    CCF201612-2 工资计算(100分)
    CCF201612-2 工资计算(100分)
    CCF201609-2 火车购票(100分)
    CCF201609-2 火车购票(100分)
    CCF201604-2 俄罗斯方块(100分)
    CCF201604-2 俄罗斯方块(100分)
    CCF201312-2 ISBN号码(100分)
    CCF201312-2 ISBN号码(100分)
    JSP---Sql Jdbc和MySql Jdbc
    JS---正则表达式验证
  • 原文地址:https://www.cnblogs.com/zfc-java/p/6986263.html
Copyright © 2011-2022 走看看