zoukankan      html  css  js  c++  java
  • 第一个SpringMVC程序

    第一个SpringMVC程序

    Spring MVC 的核心包是:

    • spring-web
    • spring-webmvc

    目录结构:

    1、配置web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <servlet>
            <servlet-name>springmvc</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>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>
    
    • 在上述web.xml文件中主要配置了<servlet><servlet-mapping>
      • 配置了前端控制器DispatcherServlet
      • 绑定了springmvc配置文件的位置
      • load-on-startup元素中的 1 表示容器(Tomcat)在启动时立即加载这个 DispatcherServlet
      • url-pattern 元素的 /,会对所有 URL 拦截(不包括 jsp / html 等),并交由 DispatcherServlet 处理

    2、编写Spring mvc的配置文件

    <?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:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        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">
    
        <!-- 处理器映射器,以处理器 Handler 的 name 作为 url 进行匹配 -->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
    
        <!-- 处理器适配器,配置对处理器中 handleRequest() 方法的调用 -->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
    
        <!-- 视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
        
        <!-- 处理器 Handler,映射 "/hello" 请求 -->
        <bean name="/hello" class="com.xu.controller.HelloController"/>
    </beans>
    

    3、编写视图(hello.jsp)

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ page isELIgnored="false" %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>第一个MVC程序</title>
    </head>
    <body>
    <h1>${msg}</h1>
    </body>
    </html>
    

    4、编写Controller

    package com.xu.controller;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class AiController implements Controller {
        @Override
        public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg","我的第一个Spring-mvc程序");
            mv.setViewName("/WEB-INF/jsp/hello.jsp");
            return mv;
        }
    }
    

    5、测试

  • 相关阅读:
    京东Java面试题(二)
    京东Java面试题(一)
    阿里java面试题
    Java垃圾回收机制
    MyBatis面试题
    Java IO流总结
    Spring中文文档
    Vue.js实战之组件之间的数据传递
    Vue.js实战之Vuex的入门教程
    Vue系列——在vue项目中使用jQuery及其第三方插件
  • 原文地址:https://www.cnblogs.com/whitespaces/p/12450337.html
Copyright © 2011-2022 走看看