SSM三大框架整合

一、基础环境搭建

本次环境搭建在《Spring整合MyBatis》基础之上进行,我们在该部分学习中完成了Spring对MyBatis的
整合,并且完成了Dao层和Service层的代码,关于整合我们需要做的操作就是让Web容器能够读取
Spring的配置文件。

1.1、导入Jar包

这里的Jar包主要包含以下内容:
Spring相关Jar包;
SpringMVC相关Jar包;
MySQL Jar包;
c3p0相关Jar包;
MyBatis相关Jar包;
MyBatis分页插件相关Jar包;
json处理相关Jar包;
JSTL相关Jar包。

在这里插入图片描述

1.2、创建包结构

  1. com.hpe.controller :用来存放SpringMVC中的控制器;
  2. com.hpe.domain :用来存放实体类(之前已经完成);
  3. com.hpe.mapper :用来存放mapper接口及接口映射文件(之前已经完成);
  4. com.hpe.service :用来存放Service层相关类(之前已经完成)。

1.3、Spring配置文件(之前已经完成)

applicationContext.xml

<!-- 配置要扫描的包 -->
    <context:component-scan base-package="com.hpe" >
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 加载配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!-- 配置sqlSessionFactory,整合MyBatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:sqlMapConfig.xml" />
    </bean>

    <!-- 配置包扫描 -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hpe.mapper" />
    </bean>

1.4、JDBC配置文件(之前已经完成)

1.5、MyBatis配置文件(之前已经完成)

sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 配置别名 -->
    <typeAliases>
        <package name="com.hpe.domian" />
    </typeAliases>
</configuration>

在此基础之上,完成了Spring对MyBatis的整合,可以在测试类中通过加载Spring配置文件进行相关测试。

二、Web层相关代码

2.1、Controller相关代码

2.1.1、RoleController

RoleController.java

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
//显示所有角色,运行完成后跳转到role-list.jsp
    @RequestMapping("/list")
    public ModelAndView list(ModelAndView mv){
        List<Role> roles = roleService.list();
        mv.addObject("roles", roles);
        mv.setViewName("user-add");
        return mv;
    }
}

2.1.2、UserController
UserController.java

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    //展示所有的user
    @RequestMapping("/list")
    public ModelAndView list(ModelAndView mv){
        List<User> users = userService.list();
        //将users传入user-list.jsp
        mv.addObject("users", users);
        mv.setViewName("user-list");
        return mv;
    }
    //跳转到user-add.jsp并显示所有角色
    @RequestMapping("/toUser")
    public ModelAndView touser(ModelAndView mv){
        List<Role> roles = roleService.list();
        mv.addObject("roles", roles);
        mv.setViewName("user-add");
        return mv;
    }

    //添加用户,运行完成后跳转到user-list.jsp,注意最后的写法
    @RequestMapping("/adduser")
    public String add(User user,Long[] roleId){

        userService.save(user, roleId);

        return "redirect:/user/list";
    }

    //删除用户,运行完成后跳转到user-list.jsp,注意最后的写法
    @RequestMapping("/del/{uid}")
    public String del(@PathVariable Long uid){

        userService.delete(uid);

        return "redirect:/user/list";
    }
}

2.2、SpringMVC配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--包扫描-->
    <context:component-scan base-package="com.hpe.controller" />
    <!--springmvc注解驱动-->
    <mvc:annotation-driven />

    <mvc:default-servlet-handler />
    <!--视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="prefix" value="/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

2.3、页面代码

以下是相关页面需要修改的地方。

<li><a href="${pageContext.request.contextPath}/user/list"> 
<i class="fa fa-circle-o"></i> 用户管理</a></li>

user-list.jsp

<script type="application/javascript">
		function del(uid) {
			if(confirm("您确定要删除该用户吗?")){
			    location.href = "${pageContext.request.contextPath}/user/del/"+uid;
			}
        }
	</script>
<button type="button" class="btn btn-default" title="新建"
onclick="location.href='${pageContext.request.contextPath}/user/toUser'">
<i class="fa fa-file-o"></i> 新建
</button>
<tbody>
<c:forEach items="${users}" var="user">
<tr>
<td><input name="ids" type="checkbox" value="${item.id}"></td>
<td>${user.id}</td>
<td>${user.username}}</td>

user-add.jsp

<form action="${pageContext.request.contextPath}/user/adduser" method="post">
<div class="col-md-2 title">用户角色</div>
						<div class="col-md-10 data">
							<c:forEach items="${roles}" var="role">
								<input type="checkbox" name="roleId" value="${role.id}">${role.roleName}
							</c:forEach>
						</div>

2.4、Web.xml配置

 <!--配置解决全站乱码的过滤器-->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>



    <!--前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--spring的监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--全局参数初始化-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值