A. java SSH框架搭建流程
首先,明确spring,struts,hibernate在环境中各自的作用。
struts:
用来响应用户的action,对应到相应的类进行处理。需要struts对应的包。
hibernate:
用来把实体类对应到数据库。提供增删改查的基本操作实现。需要hibernate对应的包以及mysql的jdbc驱动包。
spring:
管理struts:在xml配置文件中为struts的action进行值注入。
管理hibernate:在xml配置文件中配置hibernate的配置信息(dataSource,sessionFactory),即不需要原来的hibernate的xml文件。为hibernate的操作注入sessionfactory属性值。
需要提供spring对应的包,除此以外,还需要提供一个整合spring与struts的包:truts2-spring-plugin-2.0.11.1.jar
下面就搭建步骤进行详细说明:
1、新建一个web project,导入包,需要的包放在文件夹sshlib中。
2、修改web.xml的配置信息,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee "
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 ">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 下面的listener,是spring提供的,它会在创建时自动查找WEB-INF下的applicationContext.xml文件 ,从而创建spring容器-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 下面的配置作用是:在MyEclipse中,系统会自动到WEB-INF下寻找 applicationContext.xml文件,而系统
会自动将applicationContext.xml放置到WEB-INF下的classes下,所以会产生找不到applicationContext.xml的错误,需要指明applicationContext.xml
的放置位置。这就是下面的信息作用。在Eclipse中也许不需要此配置信息。-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<!-- 下面的配置信息,用来配置说明使用struts过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<!--
下面的配置信息用来说明:程序中运行的action放置在哪个包下面,对于list.action的请求,它会自动在这个包下面寻找ListAction.class的类
如果没有这句话,那么所有的action请求只能在struts.xml中进行配置。
-->
<init-param>
<param-name>actionPackages</param-name>
<param-value>
com.action
</param-value>
</init-param>
</filter>
<!--
下面的配置表示对于所有请求都交给struts来处理。
-->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3、接下来用来配置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>
<!-- 指定Web应用的默认编码集。该属性对于处理中文请求参数非常有用,对于获取中文请求参数值,应该将该属性值设置为GBK或者GB2312 当设置该参数为GBK时,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<!-- 定义拦截器 -->
<interceptor name="crudInterceptor"
class="com.action.CrudInterceptor" />
<interceptor-stack name="appStack">
<interceptor-ref name="crudInterceptor" />
<!-- 下面一行自带的拦截器必须加上,否则出错 -->
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 使用默认拦截器配置Action都需要拦截器堆栈
即所有struts请求都自动先交给拦截器处理。关于拦截器的具体规则在拦截器对应类(com.action.CrudInterceptor)中进行了解释。
-->
<default-interceptor-ref name="appStack"></default-interceptor-ref>
</package>
</struts>
4、接下来配置applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans "
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 ">
<!-- 数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="org.gjt.mm.mysql.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/user" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!-- sessionFactory配置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/ssh/User.hbm.xml</value>
</list>
</property>
<!-- 定义sessionFactory的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!-- hibernate的类名,属性名和数据库之间的对应关系的自定义 com.ynstudio.tools.LocalNamingStrategy -->
<bean id="namingStrategy"
class="org.hibernate.cfg.ImprovedNamingStrategy">
</bean>
<!-- 定义DAO的bean -->
<bean id="userDao"
class="com.ssh.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--
下面的代码用来向Action类注入属性值
-->
<bean id="crudAction" class="com.action.CrudAction">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
5、上述配置文件完成后,就开始业务逻辑部分。
首先完成hibernate的curd操作部分内容。
设计一个User实体类。包含数据库中User表的字段。
新建一个User.hbm.xml文件,实现实体类与数据库的关联。内容如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">
<hibernate-mapping>
<class name="com.ssh.User" table="user">
<id name="id" column="id">
<generator class="increment" />
</id>
<property name="username" column="username" />
<property name="password" column="password" />
<property name="birthday" column="birthday" />
<property name="email" column="email" />
</class>
</hibernate-mapping>
接下来需要实现操作。
设计一个类继承了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中配置。
* 配置时可采用如下语句:
* <action name="hello" class="crudAction">
* <result name="list">/list.jsp</result>
* <result name="modify">/modify.jsp</result>
* </action>
* 此处需要格外注意的是: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=方法名。
B. 新手如何搭建ssh架构
最近复习了下SSH框架的搭建 一:myeclipse搭建 1.添加spring支持。 在项目里右键点击 MyEclipse--AddSpring...--把AOPCoreWeb三个libraries复选上。下一步完成。 2.添加hibernate支持。 首先先打开myeclipsehibernate视图建立一个数据库连接。根据不同的数据驱动建立不同的数据库连接。 MyEclipse--AddHibernate...--根据需要一步步完成。 3.添加struts支持。 然后在web.xml文件里 context-param param-namecontexConfigLocation/param-name param-value/WEB-INF/applicationContext.xml/param-value /context-paramlistenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listener再在struts-config.xml中controllerprocessorClass=org.springframework.web.struts.DelegatingRequestProcessor /controller 这个时候加载项目会报一个确实pool包的错误。然后到构建路径里去添加起来。 这时候框架基本搭建好了。 eclipse搭建SSH步骤 1.导入驱动包。struts、spring、hibernate的包和数据库连接的驱动包。 2.配置xml文件。 3.添加struts-config.xml文件其中涉及到引用dtd文件 4.添加applicationContext.xml文件胚子该文件。(1.连接。2sessionfactory3,假如连接字符是写在属性文件里,还要配 属性文件连接的bean) 5.实体映射用到hibernate的插件。(注意表得有主键,不然生成的xml文件有错。) 6.写类biz类action等 7写JSP文件。 用eclipse搭建主要得熟悉3个配置文件的配置。(web.xml、struts-config.xml、applicationContext.xml) 不好意思,以上,希望能对你有所帮助
C. 怎么快速搭建ssh框架
第一步:创建一个叫做ssh的项目吧。勾选web.xml文件,请看如下截图。
项目部署完毕。
D. Java中SSH框架怎样搭建
方法/步骤
1
先新建一个test项目。
2
先建立与数据库的联系,在MyEclipse的右上角找到一个右上角带加号的小图标,选择Other...-->MyEclipse
Database Explorer-->在左边空白处点击右键选择New-->Database
Driver-->按下图步骤来。
3
下面开始SSH框架的搭建,SSH表示Struts2、Spring 3.0、Hibernate。现在添加Struts。项目名称点右键选择MyEclipse-->Add Struts Capabilities...
4
按照步骤3 添加好Struts后,开始添加Spring,项目名称点右键选择MyEclipse-->Add Spring Capabilities...
5
然后在WebRoot/WEB-INF/web.xml中写入代码
<!--spring 配置-->
<!-- spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- spring配置 -->
6
添加 Hibernate 包。项目名称点右键选择MyEclipse-->Add Hibernate Capabilities...
7
这样SSH框架就搭建好了。要测试的话直接运行,要是能在网页中正常显示就说明搭建的框架没有问题!比如我的test网页。这就说明我的搭建没有问题哦!
E. 怎么搭建SSH框架
1请自行安装SSH远程工具 2SSH远程登录你的linux服务器 3yum install mercurial 安装 mercurial包 安装git包 yum install git 安装gcc yum install gcc 然后就可以下载golang的压缩包了 下载完成 用tar 命令来解压压缩包 tar -zxvf go1.2.linux-a...
F. ssh框架怎么学习
先确定个开发工具,建议myEclipse
然后上网搜myEclipse搭建ssh框架,myEclipse下ssh登录实例这些关键字
找个标准且适合的源代码自己一步一步看,不懂就上网搜类似的知识
看懂了依样画葫芦照着改改,慢慢的就能明白了
PS:个人经验,自学太苦 逼了
G. 跪求SSH框架源代码
这个是本人自己在家练习ssh框架时写出来的,用的是mysql数据库。表名叫user,字段名id(主键),username(用户名),password(密码),你可以自已建表,插入几条数据用来测试。把mysql集成到Eclipse中去。这个java web项目工程我已经在MyEclipse6.5下编译运行通过,没有问题的。