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下編譯運行通過,沒有問題的。