码迷,mamicode.com
首页 > 编程语言 > 详细

HelloSpringMVC

时间:2020-01-21 00:55:56      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:except   instance   启动   mic   控制   public   技术   return   XML   

HelloSpringMVC

  1. 视图

    test.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    
    ${msg}
    
    </body>
    </html>
  2. 确保项目中有lib依赖

  3. 配置DispatcherServlet

    <!--springmvc的核心-前端控制器DispatcherServlet-->
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--    DispatcherServlet要绑定spring的配置文件-->
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc-servlet.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>
  4. spring配置文件springmvc-servlet.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--处理器映射器-->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
        <!--处理器适配器-->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
            <!--前缀-->
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <!--后缀-->
            <property name="suffix" value=".jsp"/>
        </bean>
    
        <!--BeanNameUrlHandlerMapping:bean-->
        <bean id="/hello" class="cn.pinked.controller.HelloController"/>
    </beans>
  5. 控制器

    public class HelloController implements Controller {
        public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
            ModelAndView modelAndView = new ModelAndView();
            //业务代码
            String result = "hellospringmvc";
            modelAndView.addObject("msg", result);
            //视图跳转
            modelAndView.setViewName("test");//拼接视图解析器的前后缀 
            return modelAndView;
        }
    }
  • 原理流程图

    技术图片

HelloSpringMVC

标签:except   instance   启动   mic   控制   public   技术   return   XML   

原文地址:https://www.cnblogs.com/pinked/p/12219793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!