初学者配置第一个spring mvc Demo

1.web.xml的配置

<?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"   version="3.1">
  <servlet>
  <!-- 加载前端控制器 -->
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <!-- 
       加载配置文件
       默认加载规范:
       * 文件命名:servlet-name-servlet.xml====springmvc-servlet.xml
       * 路径规范:必须在WEB-INF目录下面
       修改加载路径:
   -->
   <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:springmvc.xml</param-value>   
   </init-param>
  </servlet>

  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

这里配置将请求交给DispatcherServlet类来处理,DispatcherServlet会去找相应的handlerMapping类,默认的mapping类配置如下:

org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping

我们一个url请求handlerMapping会匹配到一个SimpleControllerHandlerAdapter的adapter,这个adapter就是用来执行我们的handler(也就是我们自己编写的controller了)

public class SimpleControllerHandlerAdapter implements HandlerAdapter {

    @Override
    public boolean supports(Object handler) {
        return (handler instanceof Controller);
    }

    @Override
    @Nullable
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        return ((Controller) handler).handleRequest(request, response);
    }

....

}

从SimpleControllerHandlerAdapter的源代码可以知道只要继承了Controller这个类,那么这个adapter就可以执行并返回一个ModelAndView(这个view对象可以设置jsp页面和model数据)对象

2.编写自己的controller

public class MyController implements Controller{

    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        ModelAndView mv = new ModelAndView();
        //设置页面回显数据
        mv.addObject("hello", "欢迎学习springmvc!使用非注解配置访问");

        //返回物理视图
        mv.setViewName("/WEB-INF/jsps/index.jsp");

        return mv;
    }
}

上面就是一个简单的controller,Controller接口就一个handleRequest方法,在里面只是new了一个简单ModelAndView对象,设置了数据和相关的jsp页面

jsp页面代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<body>
<h1>${hello}</h1>
</body>
</html>

3.springmvc.xml配置

现在jsp页面、controller也有了,用户要如何访问到我们的controller里来呢,这就需要配置url了,在web.xml配置里面有一项是关于DispatcherServlet的contextConfigLocation参数配置,我们配置的是从src的springmvc.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"
    xmlns:p="http://www.springframework.org/schema/p"
    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">
    <!-- 配置自定义Controler -->
    <bean id="/hello.do" class="com.springmvc.controller.MyController"></bean>
</beans>

通过/hello.do来找到MyController,这样就可以访问 http://localhost:8080/springMvcDemo//hello.do 来访问了

4.使用注解的方式来配置访问

在handlerMapping中默认有2个mapping上面介绍的是BeanNameUrlHandlerMapping,注解的方式用到的是RequestMappingHandlerMapping

 新建一个新的controller

@Controller
public class MyController2 {

    @RequestMapping("/hello")
    public ModelAndView hello(HttpServletRequest request,
            HttpServletResponse response) {
        ModelAndView mView=new ModelAndView();
        mView.addObject("hello", "欢迎学习springmvc!使用注解方式访问");
        mView.setViewName("/WEB-INF/jsps/index.jsp");return mView;
    }   
}

springmvc.xml再加上一行配置

<context:component-scan    base-package="com.springmvc.controller"></context:component-scan>

 测试: http://localhost:8080/springMvcDemo/hello

 

原文地址:https://www.cnblogs.com/yehuabin/p/9708192.html