构建SpringMVC应用的两种方法

2018-12-18  本文已影响33人  _于易

dispatchServlet是Spring MVC的核心。
一次Spring MVC请求的流转过程如下:

  1. 请求到达前端控制器DispatcherServlet,他的作用是将请求转发给相应的控制器(controller)。
  2. DispatcherServlet通过处理器映射得到具体是哪个控制器,然后将请求转发。
  3. 控制器进行信息的处理,然后将模型打包(模型中是后台查出来需要展示给前台的信息),并且标示出用于渲染输出的视图名。
  4. DispatcherServlet在使用视图解析器(控制器不直接传给视图解析器名称,而是通过Servlet,这样是为了使控制器和视图解析器解耦)来匹配特定的视图进行渲染。


    请求流程

1.使用XML配置

1:引入jar包
2:配置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>test</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

3:创建Controller

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
@Controller 
@RequestMapping(value="/app") 
public class LoginController{ 
  @RequestMapping(value="/welcome") //welcome要访问的url地址 
  public String hello(){ 
    System.out.println("hello,springmvc"); 
    return "hello"; //hello是逻辑视图名,和后缀名组合一起构成视图名  /web-inf/view/hello.jsp, 
   } 
}

4:新建test-servlet.xml并配置
添加扫描路径(Controller位置)

<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 
<!-- 使用注解方式完成映射 --> 
<!-- 让扫描spring扫描这个包下所有的类,让标注spring注解的类生效 -->
  <context:component-scan base-package="com.jiyunz.test" /> 
<!-- 开启注解 --> 
<!-- <mvc:annotation-driven />--> 
<!-- 视图解析器 如果不需要返回页面也可以不配置视图解析器--> 
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/view/" /> 
    <property name="suffix" value=".jsp"></property> 
  </bean>
 </beans>

5:编写JSP页面

2.使用Java类进行配置

1:配置DispatcherServlet

import com.jiyunz.test.config.WebConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

//AbstractAnnotationConfigDispatcherServletInitializer的子类都会自动地配置Dispatcher-Servlet和Spring应用上下文
//spring的应用上下文会位于程序的Servlet上下文之中
public class DispatcherServlet extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[0];
    }
    //Spring 上下文
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebConfig.class};
    }
    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }
}

Spring Web中通常会有两种应用上下文,一种是Spring应用上下文,这种上下文通过DispatcherServlet加载,对应上边的getServletConfigClasses()方法,所以必须配置其中的WebConfig类;另一种上下文不是spring的就要通过ContextLoaderListerner创建,对应的是方法getRootConfigClasses()

2:配置WebConfig

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.jiyunz.test")
public class WebConfig {

    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver resolver= new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    public void configureDefaultServleHandling(
            DefaultServletHandlerConfigurer configurer){
        configurer.enable();
    }
}

3:编写Controller
4:编写JSP文件

上一篇下一篇

猜你喜欢

热点阅读