Struts2SpringHibernate框架的简单整合2

接着http://blog.csdn.net/qq_36399629/article/details/78065184
开始web的部分的配置
首先是配置文件strus.xml与web.xml
struts.xml



<struts>
    
    <constant name="struts.objectFactory" value="spring" />
    
    <constant name="struts.action.extension" value="action" />
    
    <constant name="struts.devMode" value="true" />
    
    <constant name="struts.configuration.xml.reload" value="true" />
    
    <constant name="struts.serve.static.browserCache" value="false" />
    
    <constant name="struts.i18n.encoding" value="utf-8" />

    
    <constant name="struts.i18n.reload" value="true" />
    
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />

    

    
    <constant name="struts.tag.altSyntax" value="true" />
    
    <constant name="struts.dispatcher.parametersWorkaround" value="false" />
    
    <constant name="struts.custom.i18n.resources" value="messageResource" />
    

    <package name="backPackage" extends="struts-default"  namespace="/">
        
        <global-allowed-methods>regex:.*global-allowed-methods>
    package>
struts>

web.xml


<web-app 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_3_0.xsd"
    version="3.0">

    <welcome-file-list>
        <welcome-file>
        index.jsp
        welcome-file>
    welcome-file-list>
    
    <filter>
        <filter-name>openSessionInViewFilterfilter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilterfilter-class>
        <init-param>
            <param-name>singleSessionparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilterfilter-name>
        <url-pattern>*.actionurl-pattern>
    filter-mapping>
    
    <filter>
        <filter-name>struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>

    <filter-mapping>
        <filter-name>struts2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring.xml;classpath:spring-hibernate.xmlparam-value>
    context-param>
web-app>

接着开始添加准备在整合时候用的登陆方法
UserDaoI.java中添加

public Tuser login(String hql,Tuser user);

在UserDaoImpl中添加实现方法

    @Override
    public Tuser login(String hql, Tuser user) {

        List l = (List) getHibernateTemplate().find(hql, new String[] { user.getName(), user.getPwd() });
        if (l != null && l.size() > 0) {
            return l.get(0);
        }
        return null;
    }

然后在UserServiceI中添加要执行的方法

    public Tuser login(Tuser t);

在UserServiceImpl中开始实现该方法

    @Override
    public Tuser login(Tuser t) {
        String hql= "from tuser t where t.name=? and t.pwd = ?";
        Tuser user = userDao.login(hql,t);
        return user;
    }

然后创建Action类
UserAction

package ssh1.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

import ssh1.model.Tuser;
import ssh1.service.UserServiceI;

@ParentPackage("backPackage")
@Namespace("/")
@Action("userAction")
@Results({ @Result(name = "login", type = "redirect", location = "/index.jsp") })
public class UserAction {
    HttpServletRequest request = ServletActionContext.getRequest();

    private Tuser user = new Tuser();
    private UserServiceI userService;
    String msg;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Tuser getUser() {
        return user;
    }

    public void setUser(Tuser user) {
        this.user = user;
    }

    public UserServiceI getUserService() {
        return userService;
    }

    @Autowired
    public void setUserService(UserServiceI userService) {
        this.userService = userService;
    }
    public String login() {
        Tuser t = userService.login(user);
        if (t != null) {
            this.msg = "登陆成功";
        } else {
            this.msg = "登陆失败";
        }
        System.out.println(msg);
         request.getSession().setAttribute("msg", msg);
        return "login";
    }

}

创建jsp界面,
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"  %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <form action="${pageContext.request.contextPath}/userAction!login.action">
      name:  <input name="user.name"  /><br/>
       pass:  <input name="user.pwd" type="password" />
        <input type="submit" />
    form>
    <h1>${msg}h1>
body>
html>

运行服务器,然后输入之前测试hibernate时使用的save方法,
页面显示
Struts2SpringHibernate框架的简单整合2_第1张图片
Struts2SpringHibernate就这样配置好了

你可能感兴趣的:(java)