java web ssh框架搭建_JAVA SSH框架搭建流程

展开全部

首先,明确spring,struts,hibernate在环境中各自的作用。

struts:

用来响应用户62616964757a686964616fe78988e69d8331333339653664的action,对应到相应的类进行处理。需要struts对应的包。

hibernate:

用来把实体类对应到数据库。提供增删改查的基本操作实现。需要hibernate对应的包以及mysql的jdbc驱动包。

spring:

管理struts:在xml配置文件中为struts的action进行值注入。

管理hibernate:在xml配置文件中配置hibernate的配置信息(dataSource,sessionFactory),即不需要原来的hibernate的xml文件。为hibernate的dao操作注入sessionfactory属性值。

需要提供spring对应的包,除此以外,还需要提供一个整合spring与struts的包:truts2-spring-plugin-2.0.11.1.jar

下面就搭建步骤进行详细说明:

1、新建一个web project,导入包,需要的包放在文件夹sshlib中。

2、修改web.xml的配置信息,内容如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">

index.jsp

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

/WEB-INF/classes/applicationContext.xml

struts2

org.apache.struts2.dispatcher.FilterDispatcher

actionPackages

com.action

struts2

/*

3、接下来用来配置struts.xml

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd ">

class="com.action.CrudInterceptor" />

4、接下来配置applicationContext.xml:

xmlns:aop="http://www.springframework.org/schema/aop "

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-2.5.xsd

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

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

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

value="org.gjt.mm.mysql.Driver" />

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

com/ssh/User.hbm.xml

org.hibernate.dialect.MySQL5InnoDBDialect

update

true

true

class="org.hibernate.cfg.ImprovedNamingStrategy">

class="com.ssh.UserDaoImpl">

5、上述配置文件完成后,就开始业务逻辑部分。

首先完成hibernate的curd操作部分内容。

设计一个User实体类。包含数据库中User表的字段。

新建一个User.hbm.xml文件,实现实体类与数据库的关联。内容如下:

/p>

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

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">

接下来需要实现dao操作。

设计一个类继承了HibernateDaoSupport类。关于HibernateDaoSupport类,请参考相关文档。

6、完成hibernate的设计后,接下来设计struts的拦截器和struts的action。

struts的拦截器:

package com.action;

import java.lang.reflect.Method;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**

* 这个类的是拦截器。

* 对于如下URL:

* http://xxxxx:xxxx//xxx/hello.action?method:list

* actionInvocation.invoke()方法会自动调用名称了HelloAction类的list方法。

* 在action中根据该方法的返回值决定页面显示或跳转位置。

* result值除了可以再action类中定义,还可以在struts.xml中配置。

* 配置时可采用如下语句:

*

* /list.jsp

* /modify.jsp

*

* 此处需要格外注意的是:class属性的值,此值是applicationContext.xml中的id。

* 该bean中注入了action类中属性userDao的值。

* 所以,如果需要使用struts.xml中的action配置,需要使用该id,否则,系统不会给其注入值,最终导致空指针异常。

* @author HeXiaoXing

*

*/

public class CrudInterceptor extends AbstractInterceptor{

public String intercept(ActionInvocation actionInvocation) throws Exception {

/*

*下面代码演示了获取请求的类名与方法名的一半方式,但本例中不涉及。 全部注释掉。

*/

// Object action = actionInvocation.getAction();

// Class actionClass = action.getClass();

// String actionClassName = actionClass.getSimpleName();

// String methodName = actionInvocation.getProxy().getMethod();

return actionInvocation.invoke();

}

}

struts的action,关于此action的全部内容,请参考源程序CrudAction。

7、完成了类设计后,就是页面的设计,关于页面的设计,不再一一叙述,给粗源文件,请自行参考。

需要提出的是,在转向时,url的格式必须是method:方法名。这是约定的,不可以写成method=方法名。

本回答由电脑网络分类达人 孟男男推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值