关闭

搭建简单SSH框架经验分享

标签: ssh技术经验分享
204人阅读 评论(0) 收藏 举报
分类:

简介:

写这篇文章主要是从网上一些分享中,学到了很多知识,同时在学习的过程中也碰到了各种各样的问题,通过这篇文章把我的经验分享给大家。

希望我的分享能够带动那些技术大神们多多分享技术经验,技术是死的,人是活的,在当下互联网这个时代你的一点分享可能会解决掉一大批技术人的问题。

文章的最后会附上我搭建的SSH环境,后面会陆续更新SSH有关的技术内容,请打架多多关注。


360云盘开发工具下载:

Mysql5.6:http://yunpan.cn/ccJpvUjEjQwyK  访问密码 1641

Tomcat-7.0.62:http://yunpan.cn/ccJpGJjSduCBB  访问密码 da80

Jdk1.7:http://yunpan.cn/ccJpDIHhELpXs  访问密码 3c4c

Eclipse:http://yunpan.cn/ccJpaQHUt4cm4  访问密码 e57e


一、SSH环境介绍

     操作系统:64位Win7
     开发工具:Eclipse Java EE IDE for Web Developers Release (4.5.0)
     中间件   :Tomcat-7.0.62
     SSH       :Struts2 + Hibernate3 + SpringXX
     数据库   :mysql5.6
     JDK       :jdk1.7

二、搭建环境
    1.创建Struts项目
创建工程
1.1. 在Eclipse中创建一个Dynamic Web Project,本文项目名称为SSHFrame。把Struts包.rar内的jar包解压到SSHFrame项目下的WEB-INF/lib内;把web.xml复制到WEB-INF下
1.2. 在src目录下创建相应package
1.3. 在WebContent下创建jsp文件夹,创建Register.jsp文件,并分别创建注册成功和注册失败的Success.jsp和Fail.jsp放在jsp文件夹中

操作完成后的结构如图所示:

代码源码
  • web.xml的内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>SSH Frame</display-name>

    <filter>
        <filter-name>SSH</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

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

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

  • Register.jsp的内容

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Register Page</title>
</head>
<body>
    <s:form action="register">
        <s:textfield name="user.username" label="用户名"></s:textfield>
        <s:password name="user.password" label="密码"></s:password>
        <s:select list="#{'1':'男','0':'女'}" listKey="key" listValue="value"
            name="user.gender" label="性别" value="1"></s:select>
        <s:submit value="注册"></s:submit>
    </s:form>
</body>
</html>

  • 用户表单类UserForm.java的内容

package com.test.ssh.forms;

public class UserForm {

    private String username;
    private String password;
    private int gender;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }
}

  • 注册Action类RegisterAction.java的内容

package com.test.ssh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.test.ssh.forms.UserForm;
import com.test.ssh.service.UserManager;
import com.test.ssh.serviceImpl.UserManagerImpl;

public class RegisterAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

    private UserForm user;
    private UserManager userManager;

    public UserForm getUser() {
        return this.user;
    }

    public void setUser(UserForm user) {
        this.user = user;
    }

    public UserManager getUserManager() {
        return userManager;
    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }

    public String execute() {
        try {
            this.setUserManager(new UserManagerImpl());
            
            userManager.registerUser(this.user);
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    }
}

  • 验证文件RegisterAction-validation.xml的内容。该xml文件为RegisterAction的验证文件,命名格式固定,为< ActionName >-validation.xml

<?xml version="1.0" encoding="GBK"?>  
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"  
    "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">  
 
<validators>  
    <!-- 添加对用户名的校验 -->  
    <field name="user.username">  
        <field-validator type="requiredstring">  
            <param name="trim">true</param>  
            <message>用户名不能为空</message>  
        </field-validator>  
        <field-validator type="regex">  
            <param name="expression"><![CDATA[(\w{6,16})]]></param>  
            <message>用户名输入不合法,必须为长度在6~16中间的数字或字母</message>  
        </field-validator>
    </field>
      
    <!-- 添加对密码的校验 -->  
    <field name="user.password">  
        <field-validator type="requiredstring">  
            <param name="trim">true</param>  
            <message>密码不能为空</message>  
        </field-validator>  
        <field-validator type="regex">  
            <param name="expression"><![CDATA[(\w{6,16})]]></param>  
            <message>密码输入不合法,必须为长度在6~16之间的数字或者字母</message>  
        </field-validator>  
    </field>
</validators>

  • 业务逻辑接口UserManaer.java的内容

package com.test.ssh.service;

import com.test.ssh.forms.UserForm;

public interface UserManager {
    public void registerUser(UserForm userForm);
}

  • 业务逻辑实现类UserManagerImpl.java的内容

package com.test.ssh.serviceImpl;

import com.test.ssh.forms.UserForm;
import com.test.ssh.service.UserManager;

public class UserManagerImpl implements UserManager {

	@Override
	public void registerUser(UserForm userForm) {

	}
}

  • 配置文件struts.xml的内容

<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>
    <include file="struts-default.xml" />
    
    <package name="test" extends="struts-default">
        <action name="register" class="com.test.ssh.action.RegisterAction">
            <result name="input">/jsp/Register.jsp</result>
            <result name="success">/jsp/Success.jsp</result>
            <result name="error">/jsp/Fail.jsp</result>
        </action>
    </package>
</struts

  • 成功jsp页Success.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Successful Page</title>
    </head>
    <body>
    	<h1>Congratulations! Successful!</h1>
    </body>
    </html>
  • 失败返回页Fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Fail Page</title>
</head>
<body>
	<h1>Sorry, Register Failed!</h1>
</body>
</html>
运行效果:

输入访问Register.jsp的地址http://localhost:9090/SSHProject/Register.jsp效果图如下:

输入用户名和密码不合法时效果图如下:

注册成功效果图如下:



      2.Struts基础上整合Hibernate
整合Hibernate
2.1. 将Hibernate包内的jar包解压到SSHFrame项目的lib目录下
2.2. 在上面Struts项目的基础上添加com.test.ssh.dao和com.test.ssh.daoImpl包。分别创建BaseDao接口和UserDao类。
2.3. 在数据库中创建对应的表user来写入要注册的用户
create table user(  
userId int auto_increment,  
userName varchar(16) not null,  
password varchar(16) not null,  
gender int not null,  
primary key(userId)  
);
2.4. 创建user表的Hibernate映射文件
2.5. 建立Hibernate配置文件。路径:src目录下
2.6. 编写Session工厂类。整合Spring后,数据源的创建和SessionFactory的创建都交给Spring去管理,就无需这个工厂类了,所以这里我临时创建在daoImpl包里面
2.7. 修改业务逻辑实现类
         效果图如下:

代码源码:
  • 接口类BaseDao.java的内容。注意:接口里的两个get/set方法是为了让实现类覆盖。这样使用BaseDao dao = new UserDao()的时候,dao实体中有这两个方法

package com.test.ssh.dao;  
  
import org.hibernate.HibernateException;  
import org.hibernate.Session;  
  
public interface BaseDao {  
  
    public void saveObject(Object obj) throws HibernateException;  
  
    public Session getSession();
  
    public void setSession(Session session);  
}

  • 实现类UserDao.java的内容

package com.test.ssh.daoImpl;  
  
import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.test.ssh.dao.BaseDao;  
  
public class UserDao implements BaseDao {  
    private Session session;  
  
    @Override  
    public Session getSession() {  
        return session;  
    }  
  
    @Override  
    public void setSession(Session session) {  
        this.session = session;  
    }  
  
    @Override  
    public void saveObject(Object obj) throws HibernateException {  
        session.save(obj);  
    }  
  
}

  • 表user的POJO类User.java

package com.test.ssh.beans;

public class User {
	
	private int userid;
	private String username;
	private String password;
	private int gender;

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getGender() {
		return gender;
	}

	public void setGender(int gender) {
		this.gender = gender;
	}
}

  • POJO类User.java的Hibernate映射文件User.hbm.xml。注:代码<generator class="native"></generator>说明将根据本地数据库的设置来创建userId,如MySQL中将表user的userId设置为自动增长,则在保存记录是,userId将自动增长。对于需要在程序中指定主键的值,则将generator的class属性设置为assigned

<?xml version="1.0" encoding='UTF-8'?>  
<!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.test.ssh.beans.User" table="user">
		<id name="userid" column="userId" type="java.lang.Integer">
			<generator class="native"></generator>
		</id>
		<property name="username" column="userName" type="java.lang.String"
			not-null="true" length="16"></property>
		<property name="password" column="password" type="java.lang.String"
			not-null="true" length="16" />
		<property name="gender" column="gender" type="java.lang.Integer"
			length="1" />
	</class>
</hibernate-mapping>

  • Hibernate配置文件Hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC  
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  
<hibernate-configuration>  
  <session-factory>  
    <!-- Database connection settings -->  
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
    <property name="connection.url">jdbc:mysql://localhost:3306/mysql20150623</property>  
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
  
    <!-- JDBC connection pool (use the built-in) -->  
    <!-- <property name="connection.pool_size">1</property> -->  
  
    <!-- SQL dialect -->  
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  
    <!-- Enable Hibernate's automatic session context management -->
    <!-- <property name="current_session_context_class">thread</property> -->  
  
    <!-- Disable the second-level cache  -->  
    <!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->  
  
    <!-- Echo all executed SQL to stdout -->  
    <property name="show_sql">true</property>
  
    <!-- Drop and re-create the database schema on startup -->  
    <!-- <property name="hbm2ddl.auto">update</property> -->  
    <mapping resource="com/test/ssh/beans/User.hbm.xml"/>
  </session-factory>  
</hibernate-configuration>

  • 工厂类SessionFactory
package com.test.ssh.daoImpl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

	private static final String CFG_FILE_LOCATION = "/Hibernate.cfg.xml";
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
	private static final Configuration cfg = new Configuration();
	private static SessionFactory sessionFactory;
	
    static {
        try {
        	cfg.configure(CFG_FILE_LOCATION);
            sessionFactory = cfg.buildSessionFactory();
        } catch (Exception e) {
            System.err.println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
        }
    }
    
    public static void rebuildSessionFactory() {
        try {
        	cfg.configure(CFG_FILE_LOCATION);
            sessionFactory = cfg.buildSessionFactory();
        } catch (Exception e) {
            System.err.println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
        }
    }

	public static Session currentSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        
        if (session == null || !session.isOpen()) {
            if (sessionFactory == null) {
                rebuildSessionFactory();
            }
            session = (sessionFactory != null) ? sessionFactory.openSession()
                    : null;
            threadLocal.set(session);
        }
 
        return session;
	}

    public static void closeSession() {
        Session session = null;
        try {
            session = (Session) threadLocal.get();
            threadLocal.set(null);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
            	session.close();
            }
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
 
    public static Configuration getConfiguration() {
        return cfg;
    }
}


  • 修改后的业务逻辑实现类UserManagerImpl

package com.test.ssh.serviceImpl;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.test.ssh.beans.User;
import com.test.ssh.dao.BaseDao;
import com.test.ssh.daoImpl.HibernateSessionFactory;
import com.test.ssh.daoImpl.UserDao;
import com.test.ssh.forms.UserForm;
import com.test.ssh.service.UserManager;

public class UserManagerImpl implements UserManager {

	private BaseDao dao;

	private Session session;

	public UserManagerImpl() {
		dao = new UserDao();
	}

	@Override
	public void registerUser(UserForm userForm) {
		session = HibernateSessionFactory.currentSession();
		dao.setSession(session);
		// 获取事务
		Transaction ts = session.beginTransaction();
		// 构造User对象
		User user = new User();
		user.setUsername(userForm.getUsername());
		user.setPassword(userForm.getPassword());
		user.setGender(userForm.getGender());
		// 保存User对象
		dao.saveObject(user);
		// 提交事务
		ts.commit();
		// 关闭Session
		HibernateSessionFactory.closeSession();
	}
}

运行效果:
输入访问Register.jsp的地址http://localhost:9090/SSHProject/Register.jsp效果图如下:

点击注册后数据库存储数据效果图如下:

    Struts + Hibernate项目中整合Spring
整合Spring
  1. 将Spring包内jar包解压到SSHFrame项目的lib目录下
  2. 编写Spring的配置文件applicationContext.xml。路径:src目录下,需要在web.xml配置context-param指定路径。这里由于Spring配置数据源的需要,需要把c3p0-0.9.1.jar(在Hibenate包内)复制到lib不目下
  3. 修改BaseDao和UserDao。在引入Spring后,需要用Spring进行统一的事务管理,数据源和sessionFactory都交给Spring去生成,因此接口类和实现类BaseDao和UserDao都需要做相应的修改。Spring提供了HibernateDaoSupport类来完成对数据的操作,因此UserDao在实现BaseDao的同时还需要继承HibernateDaoSupport类。并将先前session的操作修改成HibernateTemplate(可通过getHibernateTemplate()方法来获得)的操作
  4. 修改业务逻辑实现类。在没有加入Spring之前,业务逻辑实现类的Session的获得,dao的实例化,以及事务的管理都是该类执行管理的。加入Spring后,这些都交给Spring去管理。该类的dao的实例化由Spring注入
  5. 修改用户注册的RegisterAction类。同样,RegisterAction类中的userManager的实例化也由Spring注入
  6. 删除Hibernate的配置文件Hibernate.cfg.xml和工厂类HibernateSesseionFactory类。他们的工作已经交给Spring去做,已经不再有用
  7. 修改web.xml,加载Spring。要想启动时加载Spring的配置文件,需要在web.xml中配置对应的监听器(listenser),并制定Spring的配置文件
  8. 修改Struts的配置文件struts.xml。把原来指定的名为register的action的class由原来的路径变为applicationContext.xml文件中该Action的id
效果图如下:

代码源码:
  • Spring的配置文件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"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  
    <!-- 定义数据源的信息 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
        destroy-method="close">  
        <property name="driverClass">  
            <value>com.mysql.jdbc.Driver</value>  
        </property>  
        <property name="jdbcUrl">  
            <value>jdbc:mysql://localhost:3306/mysql20150623</value>  
        </property>  
        <property name="user">  
            <value>root</value>  
        </property>  
        <property name="password">  
            <value>root</value>  
        </property>  
        <property name="maxPoolSize">  
            <value>80</value>  
        </property>  
        <property name="minPoolSize">  
            <value>1</value>  
        </property>  
        <property name="initialPoolSize">  
            <value>1</value>  
        </property>  
        <property name="maxIdleTime">  
            <value>20</value>  
        </property>  
    </bean>  
  
    <!--定义Hibernate的SessionFactory -->  
    <!-- SessionFactory使用的数据源为上面的数据源 -->  
    <!-- 指定了Hibernate的映射文件和配置信息 -->  
    <bean id="sessionFactory"  
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
        <property name="dataSource">
            <ref local="dataSource" />  
        </property>
        <property name="mappingResources">  
            <list>  
                <value>com/test/ssh/beans/User.hbm.xml</value>  
            </list>  
        </property>  
        <property name="hibernateProperties">  
            <props>  
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <prop key="show_sql">true</prop>  
                <prop key="hibernate.jdbc.batch_size">20</prop>  
            </props>  
        </property>  
    </bean>  
  
    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>  
  
    <bean id="baseDao" class="com.test.ssh.daoImpl.UserDao">  
        <property name="sessionFactory">  
            <ref bean="sessionFactory" />  
        </property>  
    </bean>  
  
    <!--用户注册业务逻辑类 -->  
    <bean id="userManager" class="com.test.ssh.serviceImpl.UserManagerImpl">  
        <property name="dao">  
            <ref bean="baseDao" />  
        </property>  
    </bean>  
  
    <!-- 用户注册的Action -->  
    <bean id="regAction" class="com.test.ssh.action.RegisterAction">  
        <property name="userManager">  
            <ref bean="userManager" />  
        </property>  
    </bean>  
  
    <!-- more bean definitions go here -->  
  
</beans>

  • 修改后的接口类Dao和实现类UserDao
BaseDao
<span style="font-size:12px;">package com.test.ssh.dao;  
  
import org.hibernate.HibernateException;  
  
public interface BaseDao {  
  
    public void saveObject(Object obj) throws HibernateException;  
  
}</span> 

UserDao
<span style="font-size:12px;">package com.test.ssh.daoImpl;  
  
import org.hibernate.HibernateException;  
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  
import com.test.ssh.dao.BaseDao;  
  
public class UserDao extends HibernateDaoSupport implements BaseDao {  
  
    @Override  
    public void saveObject(Object obj) throws HibernateException {  
        getHibernateTemplate().save(obj);  
    }  
  
}</span> 

  • 修改后的业务逻辑实现类UserManagerImpl
package com.test.ssh.serviceImpl;

import org.hibernate.HibernateException;
import org.springframework.beans.BeanUtils;

import com.test.ssh.beans.User;
import com.test.ssh.dao.BaseDao;
import com.test.ssh.forms.UserForm;
import com.test.ssh.service.UserManager;

public class UserManagerImpl implements UserManager {

	private BaseDao dao;

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}

	@Override
	public void registerUser(UserForm userForm) throws HibernateException {
		User user = new User();
		BeanUtils.copyProperties(userForm, user);
		dao.saveObject(user);
	}
}


  • 修改后的用户注册Action类RegisterAction

package com.test.ssh.action;

import com.opensymphony.xwork2.ActionSupport;
import com.test.ssh.forms.UserForm;
import com.test.ssh.service.UserManager;

public class RegisterAction extends ActionSupport {

    private static final long serialVersionUID = 1L;  
    
    private UserForm user;  
  
    private UserManager userManager;  
  
    public UserForm getUser() {  
        return user;  
    }  
  
    public void setUser(UserForm user) {  
        this.user = user;  
    }  
  
    public void setUserManager(UserManager userManager) {  
        this.userManager = userManager;  
    }  
  
    public String execute() {  
        try {  
            userManager.registerUser(user);  
            return SUCCESS;  
  
        } catch (Exception e) {  
            e.printStackTrace();  
            return ERROR;  
        }  
    }
}

  • 修改后的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>SSH Frame</display-name>

	<filter>
		<filter-name>SSH</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

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

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext*.xml</param-value>
	</context-param>

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

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>

  • 修改后的Struts配置文件struts.xml

<?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>
	<include file="struts-default.xml" />
	
	<package name="test" extends="struts-default">
		<action name="register" class="regAction"> 
			<result name="input">/jsp/Register.jsp</result>
			<result name="success">/jsp/Success.jsp</result>
			<result name="error">/jsp/Fail.jsp</result>
		</action>
	</package>
</struts>

运行效果:


    总结:
借鉴博客地址:点击打开链接
通过借鉴学习了如何搭建基础的SSH框架,希望对大家有所帮助。
附件下载:点击下载

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:208次
    • 积分:12
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档