其他分享
首页 > 其他分享> > Springmvc的4种搭建

Springmvc的4种搭建

作者:互联网

原生

web.xml配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--1.前端处理器:拦截相应的url请求,把请求分发给不同的控制器-->

  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <!--默认会加载(servlet-name)-servlet.xml,也可以指定文件名-->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <!--1.拦截特定后缀的url请求,如*.do,*.htm,*.action,/xxx/*.do
        2.拦截所有url请求,请使用/
        3.不能使用/*
    -->
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

xml配置
映射器配成BeanNameUrlHandlerMapping
适配器配成SimpleControllerHandlerAdapter

<?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">


    <!--1.handlerMapping:提供了映射规则,映射到后端处理器-->
    <!--映射规则:以控制器bean的名字作为url路径的一部份-->
    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

    <!--2.handlerAdaptor:要求后端控制器要实现Controller-->
    <bean id="handlerAdaptor" class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>


    <!--3.viewResovler-->
    <!--ModelAndView:逻辑视图名index,拼接前缀/WEB-INF/jsp/index.jsp,并拼接了后缀-->
    <bean id="viewResovler" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--4.后端控制器:http://localhost:8080/hello.do-->
    <bean name="/hello.do" class="controller.HelloWorldController"></bean>

</beans>

controller

package controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 该控制器实现了Controller接口,所以能被SimpleControllerHandlerAdapter调用
 */
public class HelloWorldController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        String mess = "hello world";

        ModelAndView modelAndView = new ModelAndView("index");//逻辑视图

        modelAndView.addObject("mess", mess); //模型数据

        return modelAndView;//交由视图解析器解析
    }
}

第二种
xml配置
适配器改成HttpRequestHandlerAdapter

<?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">


    <!--1.handlerMapping:提供了映射规则,映射到后端处理器-->
    <!--映射规则:以控制器bean的名字作为url路径的一部份-->
    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

    <!--2.handlerAdaptor:要求后端控制器实现HttpRequestHandler接口-->
    <bean id="handlerAdaptor" class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>


    <!--3.viewResovler-->
    <!--ModelAndView:逻辑视图名index,拼接前缀/WEB-INF/jsp/index.jsp,并拼接了后缀-->
    <bean id="viewResovler" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--4.后端控制器:http://localhost:8080/hello2.do-->
    <bean name="/hello2.do" class="controller.HelloWorldController2"></bean>

</beans>

controller

package controller;

import org.springframework.web.HttpRequestHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 返回Servlet编程,视图解析器组件将不生效
 * 注意:WEB-INF下资源只能通过控制器转发访问
 */
public class HelloWorldController2 implements HttpRequestHandler {
    @Override
    public void handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {

        String mess = "hello world2";

        httpServletRequest.setAttribute("mess", mess);

        httpServletRequest.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(httpServletRequest, httpServletResponse);

    }
}

xml配置
注解配置
映射器配成RequestMappingHandlerMapping
适配器配成RequestMappingHandlerAdapter

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">


    <!--1.handlerMapping和handlerAdaptor:使用springMVC注解方式-->
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

    <!--2.普通类都可以作为控制器-->
    <bean id="handlerAdaptor" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>


    <!--3.viewResovler-->
    <!--ModelAndView:逻辑视图名index,拼接前缀/WEB-INF/jsp/index.jsp,并拼接了后缀-->
    <bean id="viewResovler" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--4.开启IOC组件扫描-->
    <context:component-scan base-package="controller"></context:component-scan>
</beans>

cotroller
注解

package controller;


import model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 方式三:注解方式(常用)
 */
@Controller
@RequestMapping("/user")
public class HelloWorldController3 {


    /**
     * 返回值类型一:ModelAndView对像
     * @return
     */
    @RequestMapping("/hello3.do")
    public ModelAndView doBiz(){

        String mess = "hello world3";
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("mess", mess);
        return modelAndView;

    }

    @RequestMapping("/hello4.do")
    public ModelAndView doBiz2(){

        String mess = "hello world4";
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("mess", mess);
        return modelAndView;

    }

    /**
     * 返回值方式二:void返回
     * 返回Servlet编程,视图解析器组件将不生效
     * @param httpServletRequest
     * @param httpServletResponse
     */
    @RequestMapping("/hello5.do")
    public void doBiz3(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception{

        String mess = "hello world5";

        httpServletRequest.setAttribute("mess", mess);

        httpServletRequest.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(httpServletRequest, httpServletResponse);


    }


    /**
     * 返回值方式三:String返回(逻辑视图名)
     * @return
     */
    @RequestMapping("/hello6.do")
    public String doBiz4(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){

        String mess = "hello world6";

        httpServletRequest.setAttribute("mess", mess);

        return "index";//隐式转发
    }

    /**
     * 返回值方式三:String返回(逻辑视图名)
     * 注意:ModelMap不能主动创建,交由spring创建
     * @return
     */
    @RequestMapping("/hello7.do")
    public String doBiz5(){

        String mess7 = "hello world7";

        ModelMap modelMap = new ModelMap();

        modelMap.addAttribute("mess", mess7);

        return "index";
    }

    /**
     * 返回值方式三:String返回(逻辑视图名)
     * 注意:ModelMap不能主动创建,交由spring创建
     * @return
     */
    @RequestMapping("/hello8.do")
    public String doBiz8(ModelMap modelMap){

        String mess8 = "hello world8";

        modelMap.addAttribute("mess", mess8);

        return "index";
    }

    /**
     * 返回值方式三:String返回(逻辑视图名)
     * 注意:Map交由spring创建
     * @return
     */
    @RequestMapping("/hello9.do")
    public String doBiz9(Map<String,Object> map){

        String mess9 = "hello world9";
        map.put("mess", mess9);
        return "index";
    }

    /**
     * 返回值方式三:String返回(逻辑视图名)
     * 注意:Model交由spring创建
     * @return
     */
    @RequestMapping("/hello10.do")
    public String doBiz10(Model model){

        String mess10 = "hello world10";

        model.addAttribute("mess", mess10);

        return "index";

    }

    /**
     * 返回值方式三:String返回(真正视图)
     * 显式转发(forward),视图解析器将不生效
     * @return
     */
    @RequestMapping("/hello11.do")
    public String doBiz11(Model model){

        //String mess11 = "hello world11";

        //model.addAttribute("mess", mess11);

        return "forward:/WEB-INF/jsp/index.jsp";//显式转发:转发真正视图或控制器名
    }

    /**
     * 返回值方式三:String返回(真正视图)
     * 显式重定向(redirect),视图解析器将不生效
     * @return
     */
    @RequestMapping("/hello12.do")
    public String doBiz12(Model model){

        String mess12 = "hello world12";

        model.addAttribute("mess", mess12);//重定向会导致数据丢失

        return "redirect:/user/hello11.do";//显式重定向:一般重定向到控制器

    }


    /**
     * 把User对像转化为JSON字符串输出到浏览器
     * @ResponseBody作用,把Java对像转化为JSON
     * 依赖于JSON的转化工具
     * @return
     */
    @RequestMapping("/hello13.do")
    @ResponseBody
    public User doBiz13(){

        User user = new User();
        user.setId(1);
        user.setUsername("jack");
        user.setPassword("1234");

        return user;
    }

    /**
     * 把List对像转化为JSON字符串输出到浏览器
     * @ResponseBody作用,把Java对像转化为JSON
     * 依赖于JSON的转化工具
     * @return
     */
    @RequestMapping("/hello14.do")
    @ResponseBody
    public List<User> doBiz14(){

        User user = new User();
        user.setId(1);
        user.setUsername("jack");
        user.setPassword("1234");

        User user2 = new User();
        user2.setId(2);
        user2.setUsername("tom");
        user2.setPassword("1234");

        List<User> list = new ArrayList<>();

        list.add(user);
        list.add(user2);

        return list;

    }

}

第四种
将配文件简化
xml
mvc:annotation-driven省掉其他3个组件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
       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">



    <!--1.开启springMVC注解驱动,其作用相当于注册了RequestMappingHandlerAdapter和RequestMappingHandlerMapping-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--3.viewResovler-->
    <!--ModelAndView:逻辑视图名index,拼接前缀/WEB-INF/jsp/index.jsp,并拼接了后缀-->
    <bean id="viewResovler" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--4.开启IOC组件扫描-->
    <context:component-scan base-package="controller"></context:component-scan>



</beans>

标签:return,String,Springmvc,视图,mess,import,public,搭建
来源: https://blog.csdn.net/weixin_43343423/article/details/98883231