SSH整合中正確配置以及執行個體,ssh整合執行個體
mysql資料庫表已經欄位:
專案檔結構:
調試過程中要注意的事項:
①:applicationContext.xml檔案要放在src目錄下
②:容易出錯的地方如下代碼中字型為紅色處
常見錯誤請參看:http://blog.163.com/jxguo_05/blog/static/71940100201023185653156/ & http://blog.sina.com.cn/s/blog_6757442e0100xumy.html &
http://blog.csdn.net/yabushandaxue/article/details/39473593
各工具版本:struts2.3.4 、hibernate 3.6.0 spring3.1.3
jar包:如下::http://download.csdn.net/detail/sxf1997/7976797
這裡是一個簡單的利用ssh整合的頁面登陸介面
代碼如下:
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"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <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> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <span style="color:#ff0000;"><package name="Action" namespace="/" extends="struts-default"></span> <!-- 登入 --> <action name="login" class="Action.LoginServiceAction"> <result name="success">/success.jsp</result> <result name="error">/login.jsp</result> </action> </package></struts>
login.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"%><%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>Insert title here</title></head><body><h1>歡迎登入</h1><span style="color:#ff0000;"><s:form action="login.action" method="post" namespace="/"></span><s:textfield name="username" label="帳號"></s:textfield><s:password name="password" label="密碼"></s:password><s:submit value="登陸"></s:submit></s:form></body></html>
success.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"%><%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>Insert title here</title></head><body><h1>歡迎你,<s:property value="username"/>登陸成功!</h1></body></html>
登陸頁面處理的action
package Action;import java.util.List;import org.springframework.context.support.ClassPathXmlApplicationContext;import DAO.UserDao;import PO.User;import com.opensymphony.xwork2.ActionSupport;public class LoginServiceAction extends ActionSupport {/** * */private static final long serialVersionUID = 1L;/** * */private String username;private String password;<span style="color:#ff0000;">ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");</span>UserDao userDao=(UserDao) ctx.getBean("userDao");/** * @return the username */public String getUsername() {return username;}/** * @param username the username to set */public void setUsername(String username) {this.username = username;}/** * @return the password */public String getPassword() {return password;}/** * @param password the password to set */public void setPassword(String password) {this.password = password;}/* (non-Javadoc) * @see com.opensymphony.xwork2.ActionSupport#execute() */@Overridepublic String execute() throws Exception {// TODO Auto-generated method stub//尋找帳號相符的使用者List<User>userlist=userDao.findByName(username);//使用簡化的for語句對集合進行遍曆並比較使用者的密碼for(User user : userlist){if(user.getPassword().equals(password)){return SUCCESS;}else {return ERROR;}}return ERROR;}}
處理方法的介面 ( DAO包)
package DAO;import java.util.List;import PO.User;public interface UserDao {/** * 載入User執行個體 * @參數id知道需要載入的User執行個體的主索引值 * @return 返回載入的User執行個體 * */User get(Integer id);/** * 儲存User執行個體 * @參數user知道需要儲存的User執行個體 * @return 返回剛剛儲存的User執行個體的識別屬性值 * */Integer save(User user);/** * 根據使用者名稱尋找User * @參數那麼知道查詢的使用者名稱 * @return返回使用者名稱對呀的全部User * */List<User>findByName(String name);}
實現介面的類ImplDao.UserImpl
package ImplDao;import java.util.List;import org.hibernate.SessionFactory;import org.springframework.orm.hibernate3.HibernateTemplate;import DAO.UserDao;import PO.User;public class UserDaoImpl implements UserDao {//執行個體化一個HibernateTemplate對象,用於執行持久化操作private HibernateTemplate ht=null;//Hibernate持久化操作所需SessionFactoryprivate SessionFactory sessionFactory=null;//使用者依賴注入的setter方法public void setSessionFactory(SessionFactory sessionFactory){this.sessionFactory=sessionFactory;}//初始化HibernateTemplate方法private HibernateTemplate gethiHibernateTemplate(){if(ht==null){ht=new HibernateTemplate(sessionFactory);}return ht;}@Overridepublic User get(Integer id) {// TODO Auto-generated method stub//擷取對應表中id為某個值的資料,id為主鍵索引return gethiHibernateTemplate().get(User.class, id);}@Overridepublic Integer save(User user) {// TODO Auto-generated method stubreturn (Integer)gethiHibernateTemplate().save(user);}@SuppressWarnings("unchecked")@Overridepublic List<User> findByName(String name) {// TODO Auto-generated method stubreturn (List<User>)gethiHibernateTemplate().find("from User u where u.name=?",name);}}
PO(hibernate映射資料表的欄位)
package PO;import java.io.Serializable;public class User implements Serializable{/** * */private static final long serialVersionUID = 6810795086071173792L;private Integer intId;private String name;private String password;public User(){}public User(Integer intId,String name,String password){this.intId=intId;this.name=name;this.password=password;}/** * @return the intId */public Integer getIntId() {return intId;}/** * @param intId the intId to set */public void setIntId(Integer intId) {this.intId = intId;}/** * @return the name */public String getName() {return name;}/** * @param name the name to set */public void setName(String name) {this.name = name;}/** * @return the password */public String getPassword() {return password;}/** * @param password the password to set */public void setPassword(String password) {this.password = password;}}
User.hbm.xml(PO對象的對應檔)
<?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="PO.User" table="t_use_info" catalog="test"><id name="intId" type="integer"><column name="int_id"/><generator class="increment"/></id><property name="name" type="string"><column name="name" length="32" not-null="true"/></property><property name="password" type="string"><column name="password" length="32" not-null="true"/></property></class></hibernate-mapping>
hibernate和spring3的配置(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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- default-autowire="byName" --><!-- 定義資料庫資料來源 --> <bean id="abcd" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost/test</value> </property> <property name="username"> <value>test</value> </property> <property name="password"> <value>test</value> </property> </bean> <!-- 定義會話工廠 ,並注入資料來源執行個體dataSource --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="abcd"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="mappingResources"> <list> <value>PO/User.hbm.xml</value> </list> </property> </bean> <!-- HibernateTemplate類是簡化Hibernate資料存取碼的輔助類,可以擷取一個session對象 --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory"><ref bean="sessionFactory"/></property><property name="allowCreate"><value>true</value></property></bean><!-- 依賴注入 --><bean id="userDao" class="ImplDao.UserDaoImpl"><!-- 注入持久化操作所需的sessionfactory --><property name="sessionFactory"><ref bean="sessionFactory"/></property></bean></beans>
登陸效果:
要一個ssh整合配置舉例
struts-config.xml中的配置:
(順序:controller在message-resources 元素上面,plug-in在message-resources 下面[xml文法順序])
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>
Spring applicationContext.xml配置:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!-- 事務攔截器bean需要依賴注入一個交易管理員 -->
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<!-- 下面定義事務傳播屬性-->
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
<......餘下全文>>
ssh架構整合 沒有嚴格的順序,而在於正確配置?
嗯 是這樣的 配置對了 就容易了 包還要導的對。。至於哪個先哪個後看個人習慣吧