SpringMVC框架基础详解(三) (域对象共享数据、SpringMVC的视图、RESTFul简单使用)


一、域对象共享数据

1.1、使用ServletAPI向request域对象共享数据

@Controller
public class TestConteoller {
    @RequestMapping("/")
    public String index(){
        return "index";
    }
}





@Controller
public class ScopeController {
    //使用servletAPI向request域对象共享数据
    @RequestMapping("/testRequestByServletAPI")
    public String  testRequestByServletAPI(HttpServletRequest request){
        //往域对象共享数据
        request.setAttribute("testRequestScope","hello,sevletApI");
        return "success";
    }
}
index.html
<body>
<h1>首页</h1>
<a th:href="@{/testRequestByServletAPI}">通过sevletAPI向request域对象共享数据</a>
</body>
success.html
<body>
<h1>success!!!</h1><br>
<p th:text="${testRequestScope}"></p>
</body>

在这里插入图片描述

1.2、使用ModelAndView向request域对象共享数据

@RequestMapping("/testModelAndView")
    public ModelAndView testModelAndView(){
        ModelAndView mav=new ModelAndView();
        //处理模型数据,即向请求域request共享数据
        mav.addObject("testRequestScope","hello, ModerAndView");
        //设置视图名称
        mav.setViewName("success");
        return mav;
    }
<a th:href="@{/testModelAndView}">通过ModerAndView向request域对象共享数据</a>

在这里插入图片描述

1.3、使用Model向request域对象共享数据


    @RequestMapping("/testModel")
    public String testModel(Model model){
       model.addAttribute("testRequestScope","hello, model");
        return "success";
    }

<a th:href="@{/testModel}">通过Model向request域对象共享数据</a>

在这里插入图片描述

1.4、使用map向request域对象共享数据

@RequestMapping("/testMap")
    public String testMap(Map<String,Object> map){
        map.put("testRequestScope","hello map");
        return "success";
    }
<br>
<a th:href="@{/testMap}">通过Map向request域对象共享数据</a>

在这里插入图片描述

1.5、使用ModelMap向request域对象共享数据


    @RequestMapping("/testModelMap")
    public String testModelMap(ModelMap modelMap){
        modelMap.addAttribute("\"testRequestScope\",\"hello ModelMap\"");
        return "success";
    }
<br>
<a th:href="@{/testModelMap}">通过ModelMap向request域对象共享数据</a>

在这里插入图片描述

1.6、Model、ModelMap、Map的关系

Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的

1.7、向session域共享数据

@RequestMapping("/testSession")
    public String testSession(HttpSession session) {
        session.setAttribute("testSessionScope", "hello , session");
        return "success";
    }
<br>
<a th:href="@{/testSession}">通过ServletAPI向session域对象共享数据</a>
<p th:text="${session.testSessionScope}"></p>

在这里插入图片描述

1.8、向application域对象共享数据

 @RequestMapping("/testApplication")
    public String testApplication(HttpSession session){
        ServletContext application = session.getServletContext();
        application.setAttribute("testApplicationScope","hello, application");
        return "success";
    }
<br>
<a th:href="@{/testApplication}">通过ServletAPI向application域对象共享数据</a>
<p th:text="${application.testApplicationScope}"></p>

在这里插入图片描述


二 SpringMVC的视图

SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户
SpringMVC视图的种类很多,默认有转发视图InternalResourceView和重定向视图RedirectView

2.1、转发视图

SpringMVC中默认的转发视图是InternalResourceView

  <a th:href="@{/testForward}">测试InternalResoureView</a>
  @RequestMapping("/testForward")
    public String tstForward(){
        return "forward:/testThymeleafView";
    }
 @RequestMapping("/testThymeleafView")
    public String testThymeleafView(){
        return "success";
    }

在这里插入图片描述

2.2、重定向视图

<a th:href="@{/testRedirect}">测试testRedirectView</a>
    @RequestMapping("/testRedirect")
    public String testRedirect(){
        return "redirect:/testThymeleafView";
    }
  @RequestMapping("/testThymeleafView")
    public String testThymeleafView(){
        return "success";
    }

在这里插入图片描述

2.3 视图控制器

<!--path:设置处理的请求地址 view-name:设置请求地址所对应的视图名称 -->
 <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
    <!--开启mvc的注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>
在springMVC中配置了以上,则可以访问页面如下:

在这里插入图片描述

在这里插入图片描述

三、使用restful模拟get和post请求

@Controller
public class UserController {
    /**
     * Description:使用restful模拟用户资源的增删改查
     *
     * @date:2022/4/4 16:09
     * /user     GET      查询所有用户信息
     * /user/1   GET      根据用户id查询用户信息
     * /user/    POST     添加用户信息
     * /user/1   DELETE   删除用户信息
     * /user     PUT      修改用户信息
     */
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String getAllUser() {
        System.out.println("查询所有用户信息");
        return "success";
    }


    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public String getUserById() {
        System.out.println("根据id查询用户信息");
        return "success";
    }

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public String insertUser(String username,String password){
        System.out.println("添加用户信息:"+username+","+password);
        return "success";
    }

}
springMVC.xml文件配置页面跳转
 <mvc:view-controller path="/test_rest" view-name="test_view"></mvc:view-controller>
    <!--开启mvc的注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>

test_rest.html如下
<form th:action="@{/user}" method="post">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" name="添加"><br>
</form>

在这里插入图片描述

点击:查询所有用户信息如下:
在这里插入图片描述
在这里插入图片描述

根据id查询用户信息:
在这里插入图片描述
在这里插入图片描述
提交表单信息:
在这里插入图片描述
在这里插入图片描述

3.1、HiddenHttpMethodFilter

HiddenHttpMethodFilter 处理put和delete请求的条件:

a>  当前请求的请求方式必须为post
b>  当前请求必须传输请求参数_method
  1. 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">
    <!--配置springMVC编码过滤器-->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--配置HiddenHttpMethodFilter-->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!--springMVC前端控制器DispatcherServlet-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  1. test_rest.html 如下:

<form th:action="@{/user}" method="post">
    <input type="hidden" name="_method" value="PUT">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" name="修改"><br>
</form>
  1. UserController代码如下:

    @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public String updateUser(String username,String password){
        System.out.println("修改用户信息:"+username+","+password);
        return "success";
    }

总结:下一节:SpringMVC框架基础详解(四)RESTFul案例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值