1、Spring整合Struts2
样例:(SSHItegeration项目)
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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"> <display-name>SSHIntegeration</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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> <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> </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> <!-- Add packages here --> <constant name="struts.devMode" value="true" /> <constant name="struts.objectFactory" value="spring"></constant> <include file="struts-default.xml"></include> <package name="default" namespace="/" extends="struts-default"> <action name="login" class="loginAction"> <result>/success.jsp</result> <result name="fail">/fail.jsp</result> </action> </package> </struts>
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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="loginAction" class="com.ljb.struts.LoginAction" scope="prototype"></bean> </beans>
LoginAction.java
package com.ljb.struts; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ /** * */ private static final long serialVersionUID = -3510177794207326571L; private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute () { if (username.equals("test") && password.equals("test")) { return "success"; } else { return "fail"; } } }
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="login" method="post"> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </form> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 欢迎登陆 </body> </html>
fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 用户名或密码错误,请重新输入 </body> </html>
执行结果:
注:(如果遇到不能运行,可发信息联系)
2、spring整合hibernate
Person.java
package com.ljb.entity; public class Person { private int id; private String name; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Person.hbm.xml
<?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="com.ljb.entity.Person" table="person"> <id name="id" column="id" type="java.lang.Integer"> <generator class="assigned"/> </id> <property name="name" type="java.lang.String" column="name"/> <property name="password" type="java.lang.String" column="password"></property> </class> </hibernate-mapping>
IPersonDao.java
package com.ljb.dao; public interface IPersonDao { public boolean canLogin(String name,String password); }
PersonDaoImpl.java
package com.ljb.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; public class PersonDaoImpl implements IPersonDao { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public boolean canLogin(String name,String password) { Session session = sessionFactory.openSession(); String hql = "from Person p where p.password='"+password+"' and p.name='"+name+"'"; Transaction tx = session.beginTransaction(); Query query = session.createQuery(hql); tx.commit(); List list = query.list(); if (list.size() > 0) { return true; } return false; } }
LoginAction.java
package com.ljb.struts; import com.ljb.dao.IPersonDao; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ /** * */ private static final long serialVersionUID = -3510177794207326571L; private String username; private int id; private String password; private IPersonDao personDao; public void setPersonDao(IPersonDao personDao) { this.personDao = personDao; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute () { if (personDao.canLogin(username, password)) { return "success"; } else { return "fail"; } } }
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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/shopping</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="mappingResources"> <list> <value>com/ljb/entity/Person.hbm.xml</value> </list> </property> </bean> <bean id="personDao" class="com.ljb.dao.PersonDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="loginAction" class="com.ljb.struts.LoginAction" scope="prototype"> <property name="personDao" ref="personDao"></property> </bean> </beans>
注:页面还是用上面的