SpringMVCSpringHibernate的简单整合

前面已经弄了Spring和hibernate的整合,
详细见网址http://blog.csdn.net/qq_36399629/article/details/78065184
而加上Springmvc则是在那基础上进行处理
首先将其中struts的包移除掉,然后增添spring.webmvc的包
pom.xml


        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>

然后为springmvc添加配置文件:
spring-mvc.xml


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    
    <context:component-scan base-package="ssh1.controller" />

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />

beans>

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>

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

    
    <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>
        <description>字符集过滤器description>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <description>字符集编码description>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

    <filter-mapping>
        <filter-name>openSessionInViewFilterfilter-name>
        <url-pattern>*.dourl-pattern>
    filter-mapping>

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

    
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
    listener>
    
    <servlet>
        <description>spring mvc servletdescription>
        <servlet-name>springMvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <description>spring mvc 配置文件description>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>springMvcservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>
    
    <session-config>
        <session-timeout>15session-timeout>
    session-config>

web-app>

其中下面部分的UserDaoI,UserDaoImpl,UserServiceI,UserServiceImpl地方的更改和http://blog.csdn.net/qq_36399629/article/details/78081074
相同

将该网页之中的action下面的UserAction更改为
controller下面的UserController
代码为:


@Controller
@RequestMapping("/userController")
public class UserController {
    private Tuser user=new Tuser();
    private UserServiceI userService;
    private 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;
    }



    @RequestMapping("/login")
    public String showUser( HttpServletRequest request,Tuser user) {
        Tuser t=userService.login(user);
        System.out.println(t);
        if(t!=null) {
            this.msg="登陆成功";
        }else {
            this.msg="登陆失败";
        }

        request.getSession().setAttribute("msg", msg);
        return "redirect:/index.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}/userController/login.do">
      name:  <input name="name"  /><br/>
       pass:  <input name="pwd" type="password" />
        <input type="submit" />
    form>
    <h1>${message}h1>
body>
html>

最后开始运行,输入在前面Spring和hibernate进行整合是进行存储的账号密码,结果和http://blog.csdn.net/qq_36399629/article/details/78081074
类似

你可能感兴趣的:(java)