Struts2SpringHibernate框架的简单整合2

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <!-- 指定由Spring负责action对象的创建 -->
    <constant name="struts.objectFactory" value="spring" />
    <!-- 所有匹配*.action的请求都由struts2处理 -->
    <constant name="struts.action.extension" value="action" />
    <!-- 是否启用开发者模式 -->
    <constant name="struts.devMode" value="true" />
    <!-- struts配置文件改动后是否重新加载 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 设置浏览器是否缓存静态内容 -->
    <constant name="struts.serve.static.browserCache" value="false" />
    <!-- 请求参数的编码方式 -->
    <constant name="struts.i18n.encoding" value="utf-8" />

    <!-- 每次HTTP求情系统都重新加载资源文件 -->
    <constant name="struts.i18n.reload" value="true" />
    <!-- struts2支持动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />

    <!-- Action方法中是否用斜线 -->
<!--    <constant name="struts.enable.SlashesInActionNames" value="false" /> -->
    <!-- 允许标签中使用表达式语法 -->
    <constant name="struts.tag.altSyntax" value="true" />
    <!-- 对于weblogic,Orion,OC4j此属性应该设置为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

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <!-- openSessionInView配置 -->
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>singleSession</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <!-- 配置Struts2过滤器 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

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

    <!-- 配置Spring的监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 指定Spring配置文件所在路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml;classpath:spring-hibernate.xml</param-value>
    </context-param>
</web-app>

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

public Tuser login(String hql,Tuser user);

在UserDaoImpl中添加实现方法

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

        List<Tuser> l = (List<Tuser>) 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"  %>
<!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="${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就这样配置好了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值