Correctly configured and instance in SSH integration, and ssh integration instance
The mysql database table has fields:
Project file structure:
Precautions during debugging:
①: The applicationContext. xml file should be placed in the src directory.
②: The text in the following code is red.
Common Errors see: 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
Tool versions: struts2.3.4 and hibernate 3.6.0 spring3.1.3
Jar package: http://download.csdn.net/detail/sxf1997/7976797
Here is a simple page login interface integrated with ssh
The Code is as follows:
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> <! -- Log on --> <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 "%>
Success. jsp
<% @ Page language = "java" contentType = "text/html; charset = gb2312 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%>
Actions processed on the login page
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 // find the User List that matches the account <User> userlist = userDao. findByName (username); // use the simplified for statement to traverse the set and compare the User's password for (user: userlist) {if (User. getPassword (). equals (password) {return SUCCESS;} else {return ERROR ;}}
Processing Method interface (DAO package)
Package DAO; import java. util. list; import PO. user; public interface UserDao {/*** load User instance * @ parameter id know the primary key value of the User instance to be loaded * @ return the loaded User instance **/User get (Integer id ); /*** save the User instance ** @ parameter user know the User instance to be saved * @ return the ID attribute value of the User instance you just saved **/Integer save (User user ); /*** search for the User * @ parameter based on the User name so that you know the queried User name * @ return all the users with the correct User name **/List <User> findByName (String name );}
Implementation Interface Class 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 {// instantiate a HibernateTemplate object for executing the persistence operation private HibernateTemplate ht = null; // SessionFactoryprivate SessionFactory sessionFactory required for Hibernate Persistence operation = null; // The setter Method for user dependency injection public void setSessionFact Ory (SessionFactory sessionFactory) {this. sessionFactory = sessionFactory;} // initialize the HibernateTemplate method private HibernateTemplate gethiHibernateTemplate () {if (ht = null) {ht = new HibernateTemplate (sessionFactory);} return ht ;} @ Overridepublic User get (Integer id) {// TODO Auto-generated method stub // obtain the data whose id is a value in the corresponding table. The id is the primary key index return gethiHibernateTemplate (). get (User. class, id) ;}@ Overridepublic Integer save (User 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 (fields in the hibernate ing data table)
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 object ing file)
<?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">
Configure hibernate and 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" --> <! -- Define the database data source --> <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> <! -- Define the session factory and inject the data source instance 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 =" mappingResource S "> <list> <value> PO/User. hbm. xml </value> </list> </property> </bean> <! -- HibernateTemplate is a helper class that simplifies Hibernate data access code. You can obtain a session object --> <bean id = "hibernateTemplate" class = "org. springframework. orm. hibernate3.HibernateTemplate "> <property name =" sessionFactory "> <ref bean =" sessionFactory "/> </property> <property name =" allowCreate "> <value> true </value> </property> </bean> <! -- Dependency injection --> <bean id = "userDao" class = "ImplDao. UserDaoImpl"> <! -- Inject sessionfactory required for persistence operations --> <property name = "sessionFactory"> <ref bean = "sessionFactory"/> </property> </bean> </beans>
Login effect:
Example of an ssh integrated configuration
Configuration in struts-config.xml:
(Order: controller is on the message-resources element, and plug-in is on the message-resources [xml syntax order])
<Controller processorClass = "org. springframework. web. struts. DelegatingRequestProcessor"/>
<Plug-in className = "org. springframework. web. struts. ContextLoaderPlugIn">
<Set-property = "contextConfigLocation" value = "/WEB-INF/classes/applicationContext. xml"/>
</Plug-in>
Spring applicationContext. xml configuration:
<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">
<! -- The transaction interceptor bean needs to inject a Transaction Manager dependency -->
<Property name = "transactionManager" ref = "transactionManager"/>
<Property name = "transactionAttributes">
<! -- The following defines the transaction propagation attribute -->
<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>
& Lt ...... remaining full text>
There is no strict order for ssh framework integration, but is it correct configuration?
Well, this configuration is correct, so it is easy to export the correct package .. As for which one should first be followed by my habits