• springMVC学习三 注解开发环境搭建


    第一步:导入jar包

    第二步:配置DispatcherServlet  前端控制器

      因为此处把DsipatcherServlet的映射路径配置成了"/",代表除了.jsp文件之外,所有的请求都会被DispatcherServlet拦截,但是我们并不希望静态资源被DispatcherServlet拦截,因此在后面Springmvc的配置文件中需要配置放行静态资源(html,css,js)。

      要处理中文乱码问题,还要配置一个CharacterEncodingFilter过滤器

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <servlet>
          <!-- springMVC配置文件的命名规则为xxx-serlvet.xml,xxx代表servlet的名称,默认情况下,xxx-servlet.xml文件
              放在WEB-INF目录下,不过可以通过servlet的初始化参数进行配置
           -->
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <!-- 修改springMVC配置文件的路径和名称 -->
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:springmvc.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>springmvc</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>
      
      <!-- 字符编码过滤器-->
      <filter>
          <filter-name>encoding</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>
      </filter>
      <filter-mapping>
          <filter-name>encoding</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      
    </web-app>

    第三步配置springMVC配置文件

    (1)开启注解包扫描路径:Controller也就是处理器所在的包需要被SpringMVC容器扫描,不能被Spring容器扫描。pojo或者Service可以被Spring容器扫描   <context:component-scan base-package="com.caopeng.controller"></context:component-scan>

    (2)开启SpringMVC中HandlerMapping与HandlerAdapter的注解

     <mvc:annotation-driven></mvc:annotation-driven>
    上面的注解相当于配置下面两个类
    org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

    (3) 放行静态资源

    <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
    <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
    <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>

    /js/* 代表项目下的js文件夹下的所有的文件
    /js/js** 代表项目下的js文件夹下的所有文件以及子文件夹下的所有文件
    <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
    上面的mapping代表的是请求的资源的请求路径,location请求的资源所在的服务器的路径
    下面代表:只要发现 请求路径 符合/js/**格式,就到当前项目所在的本地服务器的/js/路径下去找资源

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-4.3.xsd 
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd "> 
    
        <!-- 开启扫描注解的包
            当前的配置文件是被DispatcherServlet加载的
            注意,此处的处理器所在的包要在springMVC的配置文件中进行扫描,不能在spring的配置文件中进行
            扫描,因为处理器要注册到springMVC容器中,也就是controller所在的包需要被SpringMVC容器扫描,
            不能被Spring容器所扫描
         -->
        <context:component-scan base-package="com.caopeng.controller"></context:component-scan>
        <!-- 注解驱动 -->
        <!-- 
            上面的注解相当于下面两个类
            org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
            org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
         -->
        <mvc:annotation-driven></mvc:annotation-driven>
        <context:annotation-config />
        <!-- 放行静态资源,不拦截静态资源 -->
        <!-- /js/* 代表项目下的js文件夹下的所有的文件
            /js/js** 代表项目下的js文件夹下的所有文件以及子文件夹下的所有文件
            <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
            上面的mapping代表的是请求的资源的请求路径,location请求的资源所在的服务器的路径
            下面代表:只要发现 请求路径 符合/js/**格式,就到当前项目所在的本地服务器的/js/路径下去找资源
            
         -->
        <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
        <mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
        <mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
        
        <!-- 注册  视图解析器 
            有时候,我们为了保护页面不被别人访问,可以把页面放在WEB-INF中,
            就可以把prefix配置成"/WEB-INF/"
            【注意】视图解析器是解析处理器最后的return 的值,并非我们在前端自己输入的请求
            如果return 的视图有前缀(forward或者redirect),视图解析器用默认的,如果没有前缀,则用我们自己配置的
        -->
        <bean id="viewResolver" class=" org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/"></property>
            <property name="suffix" value=""></property>
        </bean>
    </beans>

     第四步:编写controller

    返回值最好写全路径,全路径就是以"/"开头的路径,否则就是相对路径,
    相对路径就是以相对当前方法的映射路径,例如如果返回值是"main.jsp",是相对路径,最后的绝对路径是
    "demo/main.jsp"
    @Controller
    public class DemoController {
        
        @RequestMapping("/demo")
        public String fun01(String name,int age) {//字符串的返回值代表代表要跳转的页面
            System.out.println(name);
            System.out.println(age);
            System.out.println("指定了demo");
            //返回值最好写全路径,全路径就是以"/"开头的路径,否则就是相对路径,
            //相对路径就是以相对当前方法的映射路径,例如如果返回值是"main.jsp",是相对路径,最后的绝对路径是
            //"demo/main.jsp"
            return "/main.jsp";
        }
    }
  • 相关阅读:
    微信网页授权
    ci控制器与方法为何不能同名
    sql优化方案
    收藏了一些Notepad++快捷键
    获取IP地址
    php curl获取文件大小
    php图片压缩两个类
    PHP生成PDF完美支持中文,解决TCPDF乱码
    tcpdf函数
    Hibernate 配置
  • 原文地址:https://www.cnblogs.com/cplinux/p/9783842.html
Copyright © 2020-2023  润新知