Spring+Mybatis+SpringMVC+Maven+MySql (三)配置SpringMVC

源代码下载地址:http://download.csdn.net/download/qq_34288630/10206250
这里写图片描述
(1)在resource中的application.xml中添加扫描:

这里写图片描述

添加内容:

<!-- 扫描注解,过滤掉Controller -->
    <context:component-scan base-package="com.psw">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

完整application.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" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 引入jdbc配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:properties/*.properties</value>
                <!--要是有多个配置文件,只需在这里继续添加即可 -->
            </list>
        </property>
    </bean>

    <!-- 扫描注解,过滤掉Controller -->
    <context:component-scan base-package="com.psw">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 不使用properties来配置 -->
        <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/learning" />
            <property name="username" value="root" />
            <property name="password" value="christmas258@" /> -->
        <!-- 使用properties来配置 -->
        <property name="driverClassName">
            <value>${jdbc_driverClassName}</value>
        </property>
        <property name="url">
            <value>${jdbc_url}</value>
        </property>
        <property name="username">
            <value>${jdbc_username}</value>
        </property>
        <property name="password">
            <value>${jdbc_password}</value>
        </property>
    </bean>

    <!-- 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.psw.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /><!--加载dao-->
    </bean>

    <!-- 配置Mybatis的文件 ,mapperLocations配置**Mapper.xml文件位置,configLocation配置mybatis-config文件位置-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath*:com/psw/mapper/**/*.xml"/>
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
        <!--<property name="typeAliasesPackage" value="com.psw.domain"/>-->
    </bean>

    <!-- 自动扫描注解的bean -->
    <context:component-scan base-package="com.psw.service" />

</beans>

(2)在src/main/resource中添加springmvc文件夹,然后添加spring-mvc,内容如下:

这里写图片描述

spring-mvc.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/util
          http://www.springframework.org/schema/util/spring-util.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.xsd
         http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
    <!-- 开启默认的注解映射支持 -->
    <mvc:annotation-driven/>

    <!-- 静态资源映射 -->
    <mvc:resources location="/WEB-INF/js" mapping="/js/**"/>
    <mvc:resources location="/WEB-INF/css" mapping="/css/**"/>

    <!-- 扫描注解 过滤掉Service和Repository,只扫描Controller -->
    <context:component-scan base-package="com.psw" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>

    <!-- 对模型视图添加前后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

(3)配置web.xml
这里是关键,web.xml是用来启动spring、springMVC 放在src/main/webapp/WEB-INF下。内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>Archetype Created Web Application</display-name>
  <!--起始欢迎页-->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application.xml</param-value>
  </context-param>

  <context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>spring.root</param-value>
  </context-param>

  <filter>
    <filter-name>SpringEncodingFilter</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>SpringEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 日志记录 -->
  <context-param>
    <!-- 日志配置文件路径 -->
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>
  <context-param>
    <!-- 日志页面的刷新间隔 -->
    <param-name>log4jRefreshInterval</param-name>
    <param-value>6000</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <!--spingMVC的配置路径  -->
      <param-value>classpath:springmvc/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>

  <!-- 错误跳转页面 -->
  <error-page>
    <!-- 路径不正确 -->
    <error-code>404</error-code>
    <location>/WEB-INF/errorpage/404.jsp</location>
  </error-page>
  <error-page>
    <!-- 没有访问权限,访问被禁止 -->
    <error-code>405</error-code>
    <location>/WEB-INF/errorpage/405.jsp</location>
  </error-page>
  <error-page>
    <!-- 内部错误 -->
    <error-code>500</error-code>
    <location>/WEB-INF/errorpage/500.jsp</location>
  </error-page>
</web-app>

(4)创建controller
这里写图片描述

UserController代码:

package com.psw.controller;

import com.psw.domain.User;
import com.psw.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;

/**
 * @Author:peishunwu
 * @Description:
 * @Date:Created  2018/1/12
 */
@Controller
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/p")
    public String getUser(Model model) throws Exception {
        User user = userService.selectUserById(1);
        model.addAttribute("user",user);
        return "index";
    }

}

(5)创建index.jsp显示查询到的数据 在webapp -WEB-INF下view文件下创建index.jsp

这里写图片描述

index.jsp代码:

<html>
<body>
<h2>Hello World!</h2>
${user.userId}<br>
${user.userName}<br>
${user.sex}<br>
</body>
</html>

(6)运行
这里写图片描述

就此成功配置SpringMVC!

过程中遇到问题解决!

1、配置controller只在spring-mvc.xml中配置了

<!-- 扫描注解 过滤掉Service和Repository,只扫描Controller -->
    <context:component-scan base-package="com.psw" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>

导致运行后 找不到controller。

解决:
自己粗心,应该在application也配置如下:注意包名不要写错,不过idea写错包名路径会给出提示 会送个下划线:

<!-- 扫描注解,过滤掉Controller -->
    <context:component-scan base-package="com.psw">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

这样就解决了!

2、这里写图片描述

由于刚开始 return “/WEB-INF/view/index”

返回404 无法找到index 页面,后来改为return “index”; ok了

因为如下图视图模型已经添加了”/WEB-INF/view/” 以及.jsp后缀,所以直接返回 return “index”;这样就解决了
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值