SpringMVC entry - Hello, World

Getting SpringMVC

SpringMVC core components

  • DispatcherServlet Front Controller

Using XML-based configuration

General steps

  1. spring mvc base configuration
  2. xml configuration Controller, HandlerMapping component mapping
  3. xml configuration component mapping ViewResolver

  4. Pre-registration controller in xml, and marked the main configuration file path springmvc (after writing)
  5. Writing a Handler
  6. Write springmvc main configuration file, and registration on the inside Handler, then visit some of the paths and bind Handler

  • First, pre-arranged in web.xml controller (in the form of Servlet), while the method of mapping the root path to the controller, which is to be accessed directly by all the pre-controller
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <!-- 配置前置控制器 -->
  <servlet>
    <servlet-name>springMVCTest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 这里定义其配置文件路径属性,在其初始化时就设定 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <!-- 稍后在resources目录中创建springMVCCofing.xml作为配置文件 -->
      <param-value>classpath:springMVCCofing.xml</param-value>
    </init-param>
  </servlet>

<!-- 将所有访问映射到前置控制器 -->
  <servlet-mapping>
    <servlet-name>springMVCTest</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>
  • Handler definitions
//实现Controller接口
public class HandlerTest implements Controller {
    //实现其handleRequest方法,要求返回一个ModelAndView对象
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        //直接创建MAV对象
        ModelAndView mav=new ModelAndView();
        //装载模型数据,SpringMVC会自动将其挂载到request域中
        mav.addObject("message","hello,world");
        //设定逻辑视图
        mav.setViewName("test");
        return mav;
    }
}
  • Then to springMVCCofing.xmldefine the Handler Bean object, and then define a path for setting an object HandlerMapping Handler object and generating a mapping relationship, and then define a logical view for resolver view of ModelAndView Handler generated JSP page corresponds to parse pass
<?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 id="getHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <!--将对"/test"的访问转发给id等于getHandler的Bean-->
                <prop key="/test">getHandler</prop>
            </props>
        </property>
    </bean>
    <!-- 注册Handler的bean -->
    <bean id="getHandler" class="com.test.springmvctest.HandlerTest"></bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 这里的两个属性分别为前缀后缀,给逻辑视图包裹成/<逻辑视图名称>.jsp -->
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>
  • The last request access value domain in JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 开启EL语言 -->
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
  <!--访问request域中的key等于message的值-->
  ${message}
</body>
</html>

Based on the use of annotations

And the use of XML-based difference is that, according to Handler and does not require registration HandlerMapping in springmvc profile, Handler does not need to implement the Controller interface

Only springMVCCofing.xml different and HandlerTest.java

springMVCCofing.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: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">
    
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.test.springmvctest"/>
    
    <!-- 逻辑视图解析器还是要的 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

HandlerTest.java

// 标记为Handler
@Controller
public class HandlerTest{
    //绑定特定访问路径 
    @RequestMapping("/test")
    public ModelAndView getMAV(){
        ModelAndView mav=new ModelAndView();
        mav.addObject("message","hello,world");
        mav.setViewName("test");
        return mav;
    }
}

Meanwhile annotations based there are two ways to achieve MAV function

//绑定特定访问路径
@RequestMapping("/methodA")
// 这里的model直接以Model形式接受和挂载数据
public String MethodA(Model model){
  // 往Model对象中挂载模型数据
  model.addAttribute("message","hello,world");
  //返回以String形式逻辑视图
  return "test";
}
//绑定特定访问路径
@RequestMapping("/methodB")
// 这里的model以Map形式接受和挂载数据
public String MethodB(Map<String,String> mapAsModel{
  // 往Model对象中挂载模型数据
  
  mapAsModel.put("message","hello,world");
  //返回以String形式逻辑视图
  
  return "test";
}

Guess you like

Origin www.cnblogs.com/ruguod/p/11709174.html