任何东西都是有一定的套路和规律的,struts 2+hibernate 3+spring也一样,struts2 比struts1爽很多了,但目前唯一觉得不爽的标签:hibernate,http,spring,struts,User,import,org,public,user From: https://blog.51cto.com/u_14230175/5926548
是struts2 居然没什么好的IDE工具去支持设计之,比如DW8,希望以后有所改进.
这里,以一个简单的用户注册,登陆去说明这三者之间的简单组合关系,其他都可以按这样的去做:
1 建立domain层
建立包domain,里面有个user.java
public class User {
private Long id;
private String userName;
private String password;
//getter,setter.....
2)建立user.hbm.xml
省略
3) 建立dao层
建立dao包,有userdao.java
import java.util.List;
import liao.domain.User;
public interface UserDAO {
public User findById(long id);
public void save(User user);
public List<User> findByUserNameAndPassword(String userName,String password);
public List<User> findByUserName(String userName);
} 4 建立userdaoimpl.java
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import liao.dao.UserDAO;
import liao.domain.User;public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{
public User findById(long id) {
return (User)getHibernateTemplate().get(User.class, id);
} public List<User> findByUserName(String userName) {
return getHibernateTemplate().find("from User where userName=?",new String[]{userName});
} public List<User> findByUserNameAndPassword(String userName, String password) {
return getHibernateTemplate().find("from User where userName=? and password=?",new String[]{userName,password});
} public void save(User user) {
getHibernateTemplate().saveOrUpdate(user);
}}
5 建立sevrice层,建立好service包
import edu.jlu.fuliang.domain.User;
public interface UserManageService {
public void register(User user);
public boolean login(User user);
} 6 service的实现
import java.util.List;
import liao.Service.UserManageService;
import liao.dao.UserDAO;
import liao.domain.User;public class UserManageServiceImpl implements UserManageService{
private UserDAO userDAO;
public boolean login(User user) {
List<User> users = userDAO.findByUserNameAndPassword(user.getUserName(), user.getPassword()) ;
return users.size() != 0;
} public void register(User user) {
userDAO.save(user);
}
public UserDAO getUserDAO() {
return userDAO;
} public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}}
7 之后就是struts2的action层了
首先是注册registeraction.java
import com.opensymphony.xwork2.ActionSupport;
import liao.Service.UserManageService;
import liao.domain.User;public class RegisterAction extends ActionSupport{
private User user;
private UserManageService userManageService;
public String execute()throws Exception{
userManageService.register(user);
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public void setUserManageService(UserManageService userManageService) {
this.userManageService = userManageService;
}
} 登陆的loginaction.java
public class LoginAction extends ActionSupport{
private User user;
private UserManageService userManageService;
public String execute()throws Exception{
if(userManageService.login(user)){
ServletActionContext.getContext().getSession().put("user",user);
return SUCCESS;
}
return LOGIN;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public void setUserManageService(UserManageService userManageService) {
this.userManageService = userManageService;
}
} 8 配置好各类的applcaition.xml,建议分开几个来配置,避免全部放在一个xml
application-db.xml:比较传统,不说了
application-dao.xml:
<bean id="userDAO" class="liao.dao.impl.UserDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
application-service.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userManageService" class="liao.Service.impl.UserManageServiceImpl">
<property name="userDAO">
<ref bean="userDAO"/>
</property>
</bean>
<aop:config>
<aop:pointcut id="serviceOperation" expression="execution(* edu.jlu.fuliang.Service.impl.*ServiceImpl.*(..))"/>
<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
9 struts.xml
<action name="login" class="loginAction">
<result name="success">/uploadSong.jsp</result>
<result name="login">/login.jsp</result>
</action>
<action name="register" class="registerAction">
<result name="success" type="redirect-action">listSong</result>
<result name="input">/register.jsp</result>
</action>
10 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name> struts-cleanup </filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>