9、ssh的集成方式1

 

 

集成方式1:核心

我们没有创建applicationContext-action.xml配置文件,在该配置文件里面让Spring去管理我们的AddUserAction,但是AddUserAction的创建却是Spring产生的,但是没有对AddUserAction进行管理,
AddUserAction中使用到的依赖对象,Spring默认会根据名称自动装配

 

在该方式中Spring创建action对象依赖于struts2-spring-plugin.jar,该struts2-spring-plugin.jar中 在struts2-spring-plugin.jar中有一个struts-plugin.xml,里面声明了action类由spring工厂创建

 上面这个属性表示的就是action由Spring来进行创建

 在我们集成struts2+spring+hibernate,也就是所谓的S2SH,不可避免的要引入struts2-spring-plugin.jar插件。当引入这个插件后,原先所struts创建的action类,交给了spring创建。在struts2-spring-plugin.jar中有一个struts-plugin.xml,里面声明了action类由spring工厂创建

 

我们来看下具体的操作步骤:

 

集成Struts2+Spring+Hibernate

第一种方案:让Spring创建Struts2的Action,不让Spring完全管理Struts2的Action
Struts2 Action中的依赖对象,默认会根据名称自动装配

1、创建web项目

2、引入Struts2的依赖包,将依赖包拷贝到WEB-INF/lib下
* commons-logging-1.0.4.jar
* freemarker-2.3.15.jar
* ognl-2.7.3.jar
* struts2-core-2.1.8.1.jar
* xwork-core-2.1.6.jar
* commons-fileupload-1.2.1.jar

3、引入Spring的依赖包,将依赖包拷贝到WEB-INF/lib下
* spring.jar
* lib/aspectj/*.jar

4、引入hibernate相关依赖包,将依赖包拷贝到WEB-INF/lib下
* hibernate3.jar
* lib/*.jar

5、数据库驱动
* MySQl JDBC Driver

6、将Struts2和Spring集成的依赖包拷贝到WEB-INF/lib下
* struts2-spring-plugin-2.1.8.1.jar

7、在web.xml文件中配置StrutsPrepareAndExecuteFilter
<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>

8、提供struts.xml配置文件,提供必要属性的配置
* struts.i18n.encoding=GB18030
* struts.configuration.xml.reload=true
* struts.devMode=true

9、提供Spring的配置文件
* applicationContext-service.xml
* applicationContext-dao.xml
* applicationContext-common.xml

10、提供hibernate.cfg.xml配置文件,提供log4j

11、在web.xml文件中配置Spring的ContextLoaderListener,创建BeanFactory
<context-param>
<param-name>contextConfigLocation</param-name>
<!--
<param-value>classpath:applicationContext-*.xml</param-value>
-->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

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

11、在web.xml文件中配置OpenSessionInViewFilter(需要放到Struts2的Filter前面)
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

12、提供用户添加表单和add_success.jsp

13、建立User实体类,编写hibernate映射文件,将映射文件加入到hibernate.cfg.xml中

14、建立UserDao和UserService,并进行实现

15、建立Struts2的Action,并配置到Struts2的配置文件中

16、在jsp中调用Action

17、我们没有创建applicationContext-action.xml配置文件,在该配置文件里面去管理我们的AddUserAction,但是AddUserAction的创建却是Spring产生的,但是没有对AddUserAction进行管理,
AddUserAction中使用到的依赖对象,Spring默认会根据名称自动装配


我们来看下整个项目的视图

 首先来看配置文件

注意点:

第一hibernate的配置文件:

hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        
        <mapping resource="/com/weiyuan/test/domain/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

 

表示要连接的数据库是test,用户名是root,密码是123456
如果使用的mysql必须配置 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

 <property name="hibernate.show_sql">true</property>表示在打印台输出创建的sql语言

Hibernate配置文件的hbm2ddl.auto属性

问题就出在我把hbm2ddl.auto的值设为create了,根据Hibernate的文档,hbm2ddl.auto有四个可选值:

值 定义
update 最常用的值,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会
validate 每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值
create 每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行
create-drop 每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除

<mapping resource="/com/weiyuan/test/domain/User.hbm.xml"/> 表示通过实体类和数据库映射的配置文件,表示把/com/weiyuan/test/domain/User映射到数据库中的字段

当程序运行的时候,会自动运行加载hibernate.cfg.xml,和数据库进行连接,然后通过mapping 对于的配置文件,在数据库中创建对应的数据库表

2、配置Spring的配置文件

先在web.xml中进行配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SSH_01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 需要配置在struct2的OpenSessionInView要放在struts2的过滤器的前面 -->
      <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  
      <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的配置文件 -->
        <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- 
        <param-value>classpath:applicationContext-*.xml</param-value>
         -->
        <param-value>classpath:applicationContext.xml</param-value> 
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

 

web.xml配置了三种:

1、配置了struct2

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

2、配置了spring

<!-- 配置spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--
<param-value>classpath:applicationContext-*.xml</param-value>
-->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

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

3、配置了OpenSessionInViewFilter

该类主要实现Hibernate延迟加载,但是必须注意的是在web.xml中该类配置的顺序必须在配置struct2之前才有效果

不能写成



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

 

<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

4、我们来分析下web.xml中对spring配置文件的理解

<!-- 配置spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 
<param-value>classpath:applicationContext-*.xml</param-value>
-->
<param-value>classpath:applicationContext.xml</param-value> 
</context-param>

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

 

这里<param-value>classpath:applicationContext.xml</param-value>  表示要加载的配置文件的路径在当前classpath目录下,就是src目录下

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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <import resource="applicationContext-service.xml"/>
    <import resource="applicationContext-dao.xml"/>
    <import resource="applicationContext-common.xml"/>
</beans>

在该文件夹中加载applicationContext-service.xml,applicationContext-dao.xml,applicationContext-common.xml配置文件,就是在程序运行的时候就去加载这三个配置文件
applicationContext-service.xml该配置文件主要用于spring对mvc中的业务层serivice对象进行管理,所有的Service层都需要在该配置文件中配置
applicationContext-dao.xml该配置文件主要对mvc层中的数据库dao层进行配置管理
applicationContext-common.xml主要对一些常用的公共类进行配置和管理,一些常用的Utils工具类就在该文件中进行配置

3、现在我们来看看具体的实体类,先创建实体类,对应

package com.weiyuan.test.domain;

public class User {
    private String userCode;
    
    private String userName;
    
    private String age;

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User [userCode=" + userCode + ", userName=" + userName
                + ", age=" + age + "]";
    }
    
  
}

 

然后在该名下com.weiyuan.test.domain下创建对应的hibernate的配置文件,该配置文件为

配置文件必须和实体类在同一个包名下User.hbm.xml,中User必须和实体类的名字一样,hbm.xml后面是默认的

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.weiyuan.test.domain.User" table="t_user">
        <id name="userCode">
            <generator class="assigned"/>
        </id>
        <property name="userName"/>
        <property name="age"/>
    </class>
</hibernate-mapping>

 

该文件的内容

 property 属性的值必须和实体类的属性的名称一一对应

在hibernate中一个数据模型的主键常用有三种形式:uuid、native、assigned,分别是通用唯一标识、自增、自定义。
1、uuid是系统产生的,insert数据库时相比native要快很多,但是uuid是一长串无序字符串,理论上讲查找起来会慢一点,但是不太会影响开发的。而native是数据库生成,在insert时会先计算所以会比uuid慢一点,但是在查找和删除数据时,会比较方便。
2、uuid和assigned的生成是在程序中完成的,一个是自动,一个是手动。所以在进行session.save()时,不会产生sql语句,数据库中也不会有数据。而native需要读取数据库数据才能完成自动递增,所以在执行session.save()时,就会产生相应的sql语句,数据库中就会有数据(缓存中id是有值的)。

这样当程序运行的就会在数据库中创建t_user这张表

CREATE TABLE `t_user` (
  `userCode` varchar(255) NOT NULL,
  `userName` varchar(255) default NULL,
  `age` varchar(255) default NULL,
  PRIMARY KEY  (`userCode`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

4、创建dao类

package com.weiyuan.test.dao;

import com.weiyuan.test.domain.User;

public interface UserDao {

    public void add(User user) ;
}

 

对应的实现类为:

package com.weiyuan.test.dao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.weiyuan.test.domain.User;

/**
 * 
 * 继承:HibernateDaoSupport中使用到了sessionFactroy
 * 我们需要在配置文件中使用spring自动产生sessionFactroy
 * 我们需要在配置文件中使用spring自动产生UserDaoImpl
 * */

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

    /**
     * 需要hibernate的sessionFactory将用户添加到数据库中
     * */
    @Override
    public void add(User user) {
        // TODO Auto-generated method stub
       getHibernateTemplate().save(user);
    }

}

 

该实现类继承了HibernateDaoSupport ,然后将对象插入到数据库表t_user中

  由于Hibernate集成了JDBC,所以在访问数据库时,与直接使用JDBC访问数据库相比,Hibernate在连接、访问数据库时的代码减少了很大一大半。但由此而来也相应必须增加访问Hibernate配置文件和SessionFactory、Session的打开、关闭的代码。为了解决以上相同代码出现的复用问题,Hibernate对此又进行了再一次封装,于是,幸运地出现了HibernateDaoSupport。因此,在使用HibernateDaoSupport模板对数据库访问时,更加方便、简单,特别是进行简单的增删改查。

    下面是我自己写的一点代码参考:
/**  
 * @author wifygoo  
 * 对企业信息的简单增删改查。  
 */ 
public class EnterpriseDaoImpl extends HibernateDaoSupport implements 
        EnterpriseDao {  
    /**  
     * @param Enterprise  
     * 删除某条企业信息。  
     */ 
    public void del(Enterprise enterprise) {  
        this.getHibernateTemplate().delete(enterprise);  
    }  
    /**  
     * @return 所有的企业信息。  
     * 查询所有的企业信息。  
     */ 
    @SuppressWarnings("unchecked")  
    public List<Enterprise> findAllEnterprises() {  
        String hql = "from Enterprise enterprise";  
        return this.getHibernateTemplate().find(hql);  
    }  
    /**  
     * @param Integer,企业编号。  
     * @return 某个企业信息。  
     * 通过企业编号查询企业信息。  
     */ 
    public Enterprise findEnterpriseById(Integer id) {  
        return (Enterprise) this.getHibernateTemplate().load(Enterprise.class, id);  
    }  
    /**  
     * @param Enterprise  
     * 添加企业信息。  
     */ 
    public void save(Enterprise enterprise) {  
        this.getHibernateTemplate().save(enterprise);  
    }  
    /**  
     * @param Enterprise  
     * 修改企业信息。  
     */ 
    public void update(Enterprise enterprise) {  
        this.getHibernateTemplate().update(enterprise);  
    }  
}

 

使用借助于HibernateTemplate的模板访问方式访问数据库,大大减少了代码,但是必须依赖于SessionFactory这个对象,这个SessionFactory对象的产生我们可以让Spring来给我们产生,并对该对象进行管理,我们可以在上面创建的applicationContext-common.xml来产生了管理SessionFactory这个对象

applicationContext-common.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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>
    
     <!-- 配置事务管理器 -->
     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory"/>
     </bean>
     
     <!-- 配置事务的传播特性(配置Advice) -->
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
         <tx:attributes>
             <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
             <tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
             <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
             <tx:method name="*" propagation="REQUIRED" read-only="true"/>
         </tx:attributes>    
     </tx:advice>
    
    <!-- 配置哪些类的哪些方法使用事务(配置事务边界,配置Pointcut) -->
    <aop:config>
        <aop:pointcut id="allManagerMethod" expression="execution(* com.weiyuan.test.service.*.*(..))"/>
        <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
    </aop:config> 
</beans>

 

上面这个配置文件有两个地方需要注意的:

<property name="configLocation" value="classpath:hibernate.cfg.xml"/> 加载hibernate配置文件的路径和名称是当前的src目录下

  <aop:pointcut id="allManagerMethod" expression="execution(* com.weiyuan.test.service.*.*(..))"/>事务有效的函数的名称是在所有的包名下com.weiyuan.test.service的函数调用dao层访问数据库事务都有效

 然后在applicationContext-dao.xml对dao进行配置,让Spring进行管理和配置

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

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="userDao" class="com.weiyuan.test.dao.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>            
</beans>


 

上面就是创建userDao对象的时候,因为该对象继承了HibernateDaoSupport ,创建HibernateDaoSupport 对象的时候需要使用到sessionFactory对象,所以上面ref了applicationContext-common.xml中配置的sessionFactory对象

 

5、编写Service业务操作类

 

package com.weiyuan.test.service;

import com.weiyuan.test.domain.User;

public interface UserService {

    public void add(User user);
}

 

实现类:

package com.weiyuan.test.service;

import com.weiyuan.test.dao.UserDao;
import com.weiyuan.test.domain.User;

public class UserServiceImpl implements UserService {
 
    /**
     * 记得在applicationContext-service.xml配置使用Spring自动产生UserDao类
     * */
    private UserDao userDao;    
    @Override
    public void add(User user) {
        // TODO Auto-generated method stub
       userDao.add(user);
    }
    public UserDao getUserDao() {
        return userDao;
    }
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

}

 

现在我们要在applicationContext-service.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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <bean id="userService" class="com.weiyuan.test.service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
</beans>

 

5、接下来创建文明的action

package com.weiyuan.test.web.action;

import com.weiyuan.test.domain.User;
import com.weiyuan.test.service.UserService;

public class AddUserAction {

    private User user;
    
    /**
     * 这里的名字必须和applicationContext-service.xml中配置的名字一一对应起来
     * <bean id="userService" class="com.weiyuan.test.service.UserServiceImpl">
     * */
    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;
    }

    public String execute() throws Exception{
       System.out.println(""+user.toString());
       userService.add(user);
        return "sucess";
    }
    
    
    
}

 

这里action的创建和管理就没有单独的applicationContext-action.xml对其进行配置,因为引入了

它会由spring自动创建action对象,但是不会对action对象进行管理,

我们没有创建applicationContext-action.xml配置文件,在该配置文件里面让Spring去管理我们的AddUserAction,但是AddUserAction的创建却是Spring产生的,但是没有对AddUserAction进行管理,
AddUserAction中使用到的依赖对象private UserService userService;,Spring默认会根据名称自动装配,名称必须和

这里的名字必须和applicationContext-service.xml中配置的名字一一对应起来
* <bean id="userService" class="com.weiyuan.test.service.UserServiceImpl">

文明来看看struct.xml对action的配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.i18n.encoding" value="GB18030"/>
    <constant name="struts.configuration.xml.reload" value="true"/>
    <constant name="struts.devMode" value="true"/>
    <package name="user" extends="struts-default" >
     <action name="add" class="com.weiyuan.test.web.action.AddUserAction">
     <result name="sucess">/add_success.jsp</result>
     </action>
    </package>
</struts>

 

文明来看看index.jsp文件,该文件就是程序的入口文件

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
 
  </head>
  
<body>
    <form action="add.action" method="post">
        用户代码:<input type="text" name="user.userCode"><br>
        用户姓名:<input type="text" name="user.userName"><br>
        年龄:<input type="text" name="user.age"><br>
             <input type="submit" value="添加">
    </form>
  </body>
</html>

 

添加用户成功的页面:

add_success.jsp

 代码下载地址为:

https://pan.baidu.com/s/1bVF9pG

posted on 2017-12-18 16:58  luzhouxiaoshuai  阅读(162)  评论(0编辑  收藏  举报

导航