• SpringMVC_第一个程序


    一、基本代码的完成

    补充


    1、在myeclipse中 WEB-INF下放的资源和WebRoot下的资源区别:

    WEB-INF下放到资源是不能通过浏览器直接访问的,是比较安全的,只能是后台服务端程序进行跳转的时候跳转过去,所以不能重定向到WEB-INF.

    2、在使用EL表达式的jsp页面

    <%@page isELIgnored="false" %>

    3、SpringMVC最全约束

    <?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"
            xmlns:tx="http://www.springframework.org/schema/tx"
            xmlns:mvc="http://www.springframework.org/schema/mvc"
            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/aop
            http://www.springframework.org/schema/aop/spring-aop.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">
    </beans>
    public class MyController implements Controller {
    
        public ModelAndView handleRequest(HttpServletRequest request, 
                HttpServletResponse response) throws Exception {
            // TODO Auto-generated method stub
            ModelAndView mv= new ModelAndView();
            mv.addObject("message","hello SpringMVC World!");
            mv.setViewName("/WEB-INF/welcome.jsp");
            return mv;
        }
    
    }
    MyController

    注册处理器

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4         xmlns:context="http://www.springframework.org/schema/context"
     5         xmlns:aop="http://www.springframework.org/schema/aop"
     6         xmlns:tx="http://www.springframework.org/schema/tx"
     7         xmlns:mvc="http://www.springframework.org/schema/mvc"
     8         xsi:schemaLocation="http://www.springframework.org/schema/beans
     9         http://www.springframework.org/schema/beans/spring-beans.xsd
    10         http://www.springframework.org/schema/context
    11         http://www.springframework.org/schema/context/spring-context.xsd
    12         http://www.springframework.org/schema/aop
    13         http://www.springframework.org/schema/aop/spring-aop.xsd
    14         http://www.springframework.org/schema/tx
    15         http://www.springframework.org/schema/tx/spring-tx.xsd
    16         http://www.springframework.org/schema/mvc
    17         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    18 
    19 <!-- 注册处理器 -->
    20 <bean id="/my.do" class="com.jmu.handlers.MyController"></bean>
    21 </beans>
    springmvc.xml

    二、注册中央调度器

    在web.xml中

    <!--  注册中央调度器 -->
     <servlet>
       <servlet-name>reyco</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     </servlet>
    <servlet-mapping>
      <servlet-name>reyco</servlet-name>
      <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    三、指定SpringMVC配置文件

    添加修改

     <servlet>
       <servlet-name>reyco</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         
    <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> </servlet>

    四、springMVC执行流程

    五、使用servletloadOnStartup

    目的:在Tomcat启动时直接创建当前Servlet

    在web.xml中添加

     <load-on-startup>1</load-on-startup>

    即添加修改

    !--  注册中央调度器 -->
     <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>*.do</url-pattern>
    </servlet-mapping>

    五、使用视图解析器

    修改MyController

        mv.setViewName("welcome");//welcome为逻辑视图

    welcome逻辑视图名通过内部资源适配器InternalResourceViewResolver转换成物理视图

    修改springmvc.xml

    <!-- 注册视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/WEB-INF/jsp/"></property>
      <property name="suffix" value=".jsp"></property>
    </bean>

    六、整理

     1 public class MyController implements Controller {
     2 
     3     public ModelAndView handleRequest(HttpServletRequest request, 
     4             HttpServletResponse response) throws Exception {
     5         // TODO Auto-generated method stub
     6         ModelAndView mv= new ModelAndView();
     7         mv.addObject("message","hello SpringMVC World!");
     8         mv.setViewName("welcome");//welcome为逻辑视图
     9         return mv;
    10     }
    11 
    12 }
    MyController
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4         xmlns:context="http://www.springframework.org/schema/context"
     5         xmlns:aop="http://www.springframework.org/schema/aop"
     6         xmlns:tx="http://www.springframework.org/schema/tx"
     7         xmlns:mvc="http://www.springframework.org/schema/mvc"
     8         xsi:schemaLocation="http://www.springframework.org/schema/beans
     9         http://www.springframework.org/schema/beans/spring-beans.xsd
    10         http://www.springframework.org/schema/context
    11         http://www.springframework.org/schema/context/spring-context.xsd
    12         http://www.springframework.org/schema/aop
    13         http://www.springframework.org/schema/aop/spring-aop.xsd
    14         http://www.springframework.org/schema/tx
    15         http://www.springframework.org/schema/tx/spring-tx.xsd
    16         http://www.springframework.org/schema/mvc
    17         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    18 <!-- 注册视图解析器 -->
    19 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    20  <property name="prefix" value="/WEB-INF/jsp/"></property>
    21   <property name="suffix" value=".jsp"></property>
    22 </bean>
    23 <!-- 注册处理器 -->
    24 <bean id="/my.do" class="com.jmu.handlers.MyController"></bean>
    25 </beans>
    springmvc.xml
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns="http://java.sun.com/xml/ns/javaee"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     5     id="WebApp_ID" version="2.5">
     6     <display-name>01-springmvc-primary</display-name>
     7 
     8     <!-- 注册中央调度器 -->
     9     <servlet>
    10         <servlet-name>springMVC</servlet-name>
    11         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    12         <!-- 指定springMVC配置文件的位置及文件名 -->
    13         <init-param>
    14             <param-name>contextConfigLocation</param-name>
    15             <param-value>classpath:springmvc.xml</param-value>
    16         </init-param>
    17         <load-on-startup>1</load-on-startup>
    18     </servlet>
    19     <servlet-mapping>
    20         <servlet-name>springMVC</servlet-name>
    21         <url-pattern>*.do</url-pattern>
    22     </servlet-mapping>
    23 
    24 
    25     <welcome-file-list>
    26         <welcome-file>index.jsp</welcome-file>
    27     </welcome-file-list>
    28 
    29 </web-app>
    web.xml

    七、DispatcherServlet默认配置

  • 相关阅读:
    107.JsonResponse
    106.HttpResponse对象详解
    前端学习笔记系列一:2 Vue的单文件组件
    前端学习笔记系列一:1.export default / export const
    @vue-cli的安装及vue项目创建
    Github版本控制系统
    C# 篇基础知识11——泛型和集合
    C# 篇基础知识10——多线程
    C# 篇基础知识9——特性、程序集和反射
    C# 篇基础知识8——正则表达式
  • 原文地址:https://www.cnblogs.com/hoje/p/8531796.html
Copyright © 2020-2023  润新知