• Spring MVC第一课:用IDEA构建一个基于Spring MVC, Hibernate, My SQL的Maven项目


    作为一个Spring MVC新手最基本的功夫就是学会如何使用开发工具创建一个完整的Spring MVC项目,本文站在一个新手的角度讲述如何一步一步创建一个基于Spring MVC, Hibernate, My SQL的Maven项目。

    本项目的目的:

    • 学习如何创建并配置一个基于maven的Spring MVC项目
    • 学习如何映射数据表到对象
    • 学习如何使用Hibernate操纵数据库数据
    • 学习如何使用拦截器过滤未授权的访问

    因为在使用不同的IDEA 版本、不同的Spring MVC版本或者不同Hibernate版本创建项目的时候创建方式和配置都有可能一些不一样,下面列出本文所使用的各个组件的版本,如果你所使用的版本和我的不一样,请灵活做出改变~

    • JDK: 1.8
    • IDEA Ultimate: 2016
    • Spring MVC: 4.3.3
    • Hibernate: 5.2.3

    创建一个基于Maven的Webapp项目

    • 新建一个项目:Maven -> Create from archetype -> org.apache.maven.archetypes:maven-archetype-webapp

    注意别一不小心选择了org.apche.cocoon:cocoon-22-archetype-webapp

    • 下一步主要是需要输入项目的GroupId和ArtifactId,比较简单就不上图了。
    • 下下一步也没有什么特别要设置的,可以直接再下一步。

    •  点击完成后需要稍微等一下,因为要根据Webapp框架下载一些依赖包和创建项目索引,耐心等个一两分钟,一个基础的Webapp项目就创建好了。

    新建一个运行配置

    • Running -> Edit Configurations...

    • 在Deployment选项卡里添加一个Artifacts:

    • 全部的运行配置如下:

    配置好了之后,点击那个三角按钮运行,哈哈~Hello World! 出来了。

    至此一个基本的web项目已经创建好并可以运行。

    创建数据库

    在这里我使用的是My SQL数据库,只创建一个表用于存放用户信息,里面只有三个字段:

    CREATE DATABASE demo;
    USE demo;
    CREATE TABLE user_info
    (
        id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
        user_name VARCHAR(100) NOT NULL,
        password VARCHAR(100) NOT NULL
    );
    CREATE UNIQUE INDEX table_name_id_uindex ON user_info (id);
    INSERT INTO user_info (user_name, password) VALUES ("admin", "admin"); 

    创建Spring MVC目录结构

    打开Project Structure,创建如下图红框内的文件夹:

    • java: 标记为Sources(从颜色可看出与其他文件夹的区别)
    • controller: 控制器代码,主要是提供web接口。
    • dao: 数据访问对象代码,用于操纵数据库。
    • entity: 实体类代码,对应数据库表的映射。
    • interceptor: 拦截器代码,比如可用用来拦截未经验证的请求。
    • service: 业务层代码
    • utils: 工具类代码

    配置文件

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example</groupId>
        <artifactId>maven-springmvc-hibernate-mysql-demo</artifactId>
        <packaging>war</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>maven-springmvc-hibernate-mysql-demo Maven Webapp</name>
        <url>http://maven.apache.org</url>
    
        <dependencies>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.3.2.release</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.3.2.release</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>4.3.2.release</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>4.3.2.RELEASE</version>
            </dependency>
            <!-- Hibernate -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.2.3.Final</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>5.2.3.Final</version>
            </dependency>
            <!-- unit test -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <!-- log -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.6.1</version>
            </dependency>
            <!-- jstl -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <!-- Apache Commons DBCP -->
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>1.4</version>
            </dependency>
            <!-- tomcat servlet api -->
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-servlet-api</artifactId>
                <version>7.0.53</version>
                <scope>provided</scope>
            </dependency>
            <!-- MySql 5.5 Connector -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>6.0.4</version>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>maven-springmvc-hibernate-mysql-demo</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <!--JDK version-->
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    View Code

    添加好pom文件后,点击刷新,过程中需要在线下载一些依赖包和创建索引,这中间可能需要等待较长时间,可以去泡杯茶喝先了。因为国内连接国外的Maven仓库超级不稳定,如果下载失败,请自行查找可用的Maven仓库并添加到Maven的settings.xml文件(推荐阿里云的仓库)。

    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">
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    </web-app>
    View Code

    添加配置文件:WEB-INF/applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:beans="http://www.springframework.org/schema/beans"
                 xmlns:context="http://www.springframework.org/schema/context"
                 xmlns:tx="http://www.springframework.org/schema/tx"
                 xmlns:mvc="http://www.springframework.org/schema/mvc"
                 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
        <annotation-driven/>
        <context:component-scan base-package="com.demo.controller"/>
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
        <beans:bean
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/"/>
        </beans:bean>
    
        <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                    destroy-method="close">
            <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <beans:property name="url"
                            value="jdbc:mysql://localhost:3306/demo"/>
            <beans:property name="username" value="root"/>
            <beans:property name="password" value="xxx"/>
        </beans:bean>
    
        <!-- Hibernate 5 SessionFactory Bean definition -->
        <beans:bean id="hibernate5AnnotatedSessionFactory"
                    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <beans:property name="dataSource" ref="dataSource"/>
            <beans:property name="annotatedClasses">
                <beans:list>
                    <beans:value>com.demo.entity.UserInfoEntity</beans:value>
                </beans:list>
            </beans:property>
            <beans:property name="hibernateProperties">
                <beans:props>
                    <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
                    </beans:prop>
                    <beans:prop key="hibernate.show_sql">true</beans:prop>
                </beans:props>
            </beans:property>
        </beans:bean>
    
        <beans:bean id="userInfoDao" class="com.demo.dao.UserInfoDaoImpl">
            <beans:property name="sessionFactory" ref="hibernate5AnnotatedSessionFactory"/>
        </beans:bean>
    
        <beans:bean id="userInfoService" class="com.demo.service.UserInfoServiceImpl">
            <beans:property name="userInfoDao" ref="userInfoDao"></beans:property>
        </beans:bean>
    
        <beans:bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <beans:property name="sessionFactory" ref="hibernate5AnnotatedSessionFactory"/>
        </beans:bean>
    
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/index"/>
                <mvc:mapping path="/user/**"/>
                <beans:bean class="com.demo.interceptor.LoginInterceptor"/>
            </mvc:interceptor>
        </mvc:interceptors>
    
    </beans:beans>
    View Code

     需改bean "dataSource" 中的My SQL配置,把IP、用户名、密码修改为你相应的值。

    添加配置文件:WEB-INF/dispatcher-servlet.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.xsd">
    
    </beans>
    View Code

    前端文件

    1. 把系统自动生成的index.jsp删除掉。
    2. 在/webapp/WEB-INF下创建views文件夹,然后分别创建一下两个文件:

    login.jsp

    <%--
      User: keitsi
      Date: 16-10-22
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>User Login</title>
    </head>
    <body>
    <form action="/authenticate" method="post">
        <div>
            <label for="user_name">User Name: </label>
            <input id="user_name" name="userName" type="text">
        </div>
        <div>
            <label for="password">Password: </label>
            <input id="password" name="password" type="password">
        </div>
        <div>
            <input type="submit" value="submit">
        </div>
        <p style="color: red;"><%=request.getParameter("errorMessage") == null ? "" : request.getParameter("errorMessage")%>
        </p>
    </form>
    </body>
    </html>
    View Code

    user_list.jsp

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page session="false" %>
    <html>
    <head>
        <title>User Page</title>
        <style type="text/css">
            .tg {
                border-collapse: collapse;
                border-spacing: 0;
                border-color: #ccc;
            }
    
            .tg td {
                padding: 10px 5px;
                border: solid 1px #ccc;
                text-align: center;
            }
    
            .tg th {
                border: solid 1px #ccc;
                background-color: #f0f0f0;
                padding: 10px 5px;
            }
        </style>
    </head>
    <body>
    <h3>Hello ${LoginUser.userName} <a href="/user/logout">Logout</a></h3>
    
    <br>
    
    <h3>
        Modify User
    </h3>
    
    <c:url var="addAction" value="/user/modify"></c:url>
    
    <form action="${addAction}" method="post">
        <table>
            <c:if test="${!empty user.userName}">
                <tr>
                    <td>
                        <label for="id">ID: </label>
                    </td>
                    <td>
                        <input id="id" name="id" value="${user.id}">
                    </td>
                </tr>
            </c:if>
            <tr>
                <td>
                    <label for="user_name">User Name: </label>
                </td>
                <td>
                    <input id="user_name" name="userName" value="${user.userName}">
                </td>
            </tr>
            <tr>
                <td>
                    <label for="password">Password: </label>
                </td>
                <td>
                    <input id="password" name="password" value="${user.password}">
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <c:if test="${!empty user.userName}">
                        <input type="submit" value="Edit Person"/>
                    </c:if>
                    <c:if test="${empty user.userName}">
                        <input type="submit" value="Add Person"/>
                    </c:if>
                </td>
            </tr>
        </table>
    </form>
    
    <br>
    
    <h3>User List</h3>
    <c:if test="${!empty allUsers}">
        <table class="tg">
            <tr>
                <th width="80">User ID</th>
                <th width="120">User Name</th>
                <th width="120">Password</th>
                <th width="60">Edit</th>
                <th width="60">Delete</th>
            </tr>
            <c:forEach items="${allUsers}" var="user">
                <tr>
                    <td>${user.id}</td>
                    <td>${user.userName}</td>
                    <td>${user.password}</td>
                    <td><a href="<c:url value='/user/edit/${user.id}' />">Edit</a></td>
                    <td><a href="<c:url value='/user/remove/${user.id}' />">Delete</a></td>
                </tr>
            </c:forEach>
        </table>
    </c:if>
    </body>
    </html>
    View Code

    数据库访问层

    entity/UserInfoEntity.java

    package com.demo.entity;
    
    import javax.persistence.*;
    
    /**
     * Created by keitsi on 16-10-21.
     */
    @Entity
    @Table(name = "user_info", schema = "demo")
    public class UserInfoEntity {
        private int id;
        private String userName;
        private String password;
    
        @Id
        @Column(name = "id")
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        @Basic
        @Column(name = "user_name")
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        @Basic
        @Column(name = "password")
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
    
            UserInfoEntity that = (UserInfoEntity) o;
    
            if (id != that.id) return false;
            if (userName != null ? !userName.equals(that.userName) : that.userName != null) return false;
            if (password != null ? !password.equals(that.password) : that.password != null) return false;
    
            return true;
        }
    
        @Override
        public int hashCode() {
            int result = id;
            result = 31 * result + (userName != null ? userName.hashCode() : 0);
            result = 31 * result + (password != null ? password.hashCode() : 0);
            return result;
        }
    }
    View Code

    dao/UserInfoDao.java

    package com.demo.dao;
    
    /**
     * Created by keitsi on 16-10-21.
     */
    
    import com.demo.entity.UserInfoEntity;
    
    import java.util.List;
    
    public interface UserInfoDao {
    
        void add(UserInfoEntity p);
    
        void update(UserInfoEntity p);
    
        void remove(int id);
    
        List<UserInfoEntity> getAll();
    
        UserInfoEntity getById(int id);
    
        UserInfoEntity getByName(String name);
    
    }
    View Code

    dao/UserInfoDaoImpl.java

    package com.demo.dao;
    
    /**
     * Created by keitsi on 16-10-21.
     */
    
    import com.demo.entity.UserInfoEntity;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.query.Query;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Repository;
    
    import java.util.List;
    
    @Repository
    public class UserInfoDaoImpl implements UserInfoDao {
    
        private static final Logger logger = LoggerFactory.getLogger(UserInfoDaoImpl.class);
    
        private SessionFactory sessionFactory;
    
        public void setSessionFactory(SessionFactory sf) {
            this.sessionFactory = sf;
        }
    
        public void add(UserInfoEntity user) {
            Session session = this.sessionFactory.getCurrentSession();
            session.persist(user);
            logger.info("Person saved successfully, Person Details=" + user);
        }
    
        public void update(UserInfoEntity user) {
            Session session = this.sessionFactory.getCurrentSession();
            session.update(user);
            logger.info("Person updated successfully, Person Details=" + user);
        }
    
        public void remove(int id) {
            Session session = this.sessionFactory.getCurrentSession();
            UserInfoEntity user = (UserInfoEntity) session.load(UserInfoEntity.class, new Integer(id));
            if (null != user) {
                session.delete(user);
            }
            logger.info("Person deleted successfully, person details=" + user);
        }
    
        @SuppressWarnings("unchecked")
        public List<UserInfoEntity> getAll() {
            Session session = this.sessionFactory.getCurrentSession();
            List<UserInfoEntity> userList = session.createQuery("from UserInfoEntity ").list();
            for (UserInfoEntity user : userList) {
                logger.info("Person List::" + user);
            }
            return userList;
        }
    
        public UserInfoEntity getById(int id)
        {
            Session session = this.sessionFactory.getCurrentSession();
            UserInfoEntity user = (UserInfoEntity) session.load(UserInfoEntity.class, new Integer(id));
            logger.info("Person loaded successfully, Person details=" + user);
            return user;
        }
    
        public UserInfoEntity getByName(String name){
            Session session = this.sessionFactory.getCurrentSession();
            Query query = session.createQuery("from UserInfoEntity where userName = :userName ");
            query.setParameter("userName", name);
            List list = query.list();
            if (list.size()>0){
                return (UserInfoEntity) list.get(0);
            }else {
                return null;
            }
        }
    
    }
    View Code

    业务层代码

    service/UserInfoService.java

    package com.demo.service;
    
    /**
     * Created by keitsi on 16-10-21.
     */
    
    import com.demo.entity.UserInfoEntity;
    
    import java.util.List;
    
    public interface UserInfoService {
    
        void add(UserInfoEntity p);
    
        void update(UserInfoEntity p);
    
        void remove(int id);
    
        List<UserInfoEntity> getAll();
    
        UserInfoEntity getById(int id);
    
        UserInfoEntity getByName(String name);
    }
    View Code

    service/UserInfoServiceImpl.java

    package com.demo.service;
    
    /**
     * Created by keitsi on 16-10-21.
     */
    
    import com.demo.dao.UserInfoDao;
    import com.demo.entity.UserInfoEntity;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @Service
    public class UserInfoServiceImpl implements UserInfoService {
    
        @Resource
        private UserInfoDao userInfoDao;
    
        public void setUserInfoDao(UserInfoDao userInfoDao) {
            this.userInfoDao = userInfoDao;
        }
    
        @Transactional
        public void add(UserInfoEntity user) {
            this.userInfoDao.add(user);
        }
    
        @Transactional
        public void update(UserInfoEntity user) {
            this.userInfoDao.update(user);
        }
    
        @Transactional
        public void remove(int id) {
            this.userInfoDao.remove(id);
        }
    
        @Transactional
        public List<UserInfoEntity> getAll() {
            return this.userInfoDao.getAll();
        }
    
        @Transactional
        public UserInfoEntity getById(int id) {
            return this.userInfoDao.getById(id);
        }
    
        @Transactional
        public UserInfoEntity getByName(String name)
        {
            return this.userInfoDao.getByName(name);
        }
    
    }
    View Code

    控制器层代码

    controller/RootController.java

    package com.demo.controller;
    
    import com.demo.entity.UserInfoEntity;
    import com.demo.service.UserInfoService;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Created by keitsi on 16-10-22.
     */
    @Controller
    @RequestMapping("")
    public class RootController {
        @Resource
        private UserInfoService userService;
    
        @RequestMapping("")
        public String root() {
            return "redirect:/index";
        }
    
        @RequestMapping("/index")
        public String index() {
            return "redirect:/user/list.jsp";
        }
    
        @RequestMapping("/login")
        public String login() {
            return "/login.jsp";
        }
    
        @RequestMapping(value = "/authenticate", method = RequestMethod.POST)
        public void login(HttpServletRequest request, HttpServletResponse response, String userName, String password) throws Exception {
            UserInfoEntity user = userService.getByName(userName);
            if (user.getPassword().equals(password)) {
                request.getSession().setAttribute("LoginUser", user);
                response.sendRedirect(request.getContextPath() + "/user/list");
            } else {
                response.sendRedirect(request.getContextPath() + "/login?errorMessage=User name or password is not correct.");
            }
        }
    }
    View Code

    controller/UserController.java

    package com.demo.controller;
    
    /**
     * Created by keitsi on 16-10-21.
     */
    
    import com.demo.entity.UserInfoEntity;
    import com.demo.service.UserInfoService;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
        @Resource
        private UserInfoService userService;
    
        @RequestMapping(value = "/list", method = RequestMethod.GET)
        public String listUsers(HttpServletRequest request, Model model) {
            UserInfoEntity loginUser = (UserInfoEntity)request.getSession().getAttribute("LoginUser");
            model.addAttribute("LoginUser", loginUser);
            model.addAttribute("user", new UserInfoEntity());
            model.addAttribute("allUsers", this.userService.getAll());
            return "/user_list.jsp";
        }
    
        @RequestMapping(value = "/logout", method = RequestMethod.GET)
        public void logout(HttpServletRequest request, HttpServletResponse response, String userName, String password) throws Exception {
            request.getSession().setAttribute("LoginUser", null);
            response.sendRedirect(request.getContextPath() + "/index");
        }
    
        //For add and update person both
        @RequestMapping(value = "/modify", method = RequestMethod.POST)
        public String addPerson(@ModelAttribute("user") UserInfoEntity user) {
    
            if (user.getId() == 0) {
                //new person, add it
                this.userService.add(user);
            } else {
                //existing person, call update
                this.userService.update(user);
            }
            return "redirect:/user/list";
        }
    
        @RequestMapping("/edit/{id}")
        public String editPerson(HttpServletRequest request, @PathVariable("id") int id, Model model) {
            UserInfoEntity loginUser = (UserInfoEntity)request.getSession().getAttribute("LoginUser");
            model.addAttribute("LoginUser", loginUser);
            model.addAttribute("user", this.userService.getById(id));
            model.addAttribute("allUsers", this.userService.getAll());
            return "/user_list.jsp";
        }
    
        @RequestMapping("/remove/{id}")
        public String removePerson(@PathVariable("id") int id) {
            this.userService.remove(id);
            return "redirect:/user/list";
        }
    
    }
    View Code

    拦截器层代码

    interceptor/LoginInterceptor.java

    package com.demo.interceptor;
    
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Created by keitsi on 16-10-22.
     */
    public class LoginInterceptor implements HandlerInterceptor {
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            Object loginUser = request.getSession().getAttribute("LoginUser");
            if(loginUser == null){
                response.sendRedirect(request.getContextPath() + "/login");
                return false;
            }
            return loginUser != null;
        }
    
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
                throws Exception {
        }
    
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        }
    }
    View Code

    运行

    点击运行,在启动tomcat的时候有可能会遇到如下图所示的运行异常:

    严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
    java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1891)
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1734)
        at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:504)
        at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:486)

    这是因为我们的打包的war包里面没有自动加入Spring MVC的jar包,所以只需要把依赖的jar包添加到输出目录即可:

    File -> Project Structure... -> Artifacts

    在Available Elements的根节点点击右键 -> Put into Out Root

    再次点击运行,如果不出什么故障你就能看到下面的登录界面:

    用户名和密码都是:admin

    页面比较简单,因为没有加入太多的样式。

    登录之后的界面:

    是一个用户管理界面,覆盖了对数据库的:增、删、改、查

    项目代码:https://github.com/keitsi/maven-springmvc-hibernate-mysql-demo 

    转载请注明出处:http://www.cnblogs.com/keitsi/p/5990479.html

  • 相关阅读:
    JS使用Enter事件将输入的字符倒叙输出
    博客园 页面定制CSS代码
    C#将DataTable数据导出CSV文件
    js时间格式转换
    将十进制数据转换为二进制AND将二进制数据转换为十进制!
    ref和out的使用与区别
    形参和实参的区别
    为啥Underlay才是容器网络的最佳落地选择
    你问我答:容器篇(1)
    切忌一步到位,谈谈DevOps实施落地
  • 原文地址:https://www.cnblogs.com/keitsi/p/5990479.html
Copyright © 2020-2023  润新知