SpringMVC学习(二)

25 篇文章 0 订阅
3 篇文章 0 订阅

使用注解修改第一个mvc程序

修改springmvc-servlet程序

<?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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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">

    <!-- 注解驱动 -->
    <mvc:annotation-driven/>
    <!-- 自动扫描包,让指定包下的注解生效 -->
    <context:component-scan base-package="com.wangqi.controller"/>
    <!-- 让spring MVC不处理静态资源 .css .js 等文件-->
    <mvc:default-servlet-handler/>
    <!--视图解析器:DispatcherServlet给他的ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

controller

HelloController.java

package com.wangqi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
//@RequestMapping("/hello") //使用此注解会在下面方法的路径前添加一个/hello
public class HelloController {

    @RequestMapping("/hello")
    public String Hello(Model model) {
        //封装数据
        model.addAttribute("msg", "Hello, SpringMVCAnnotation");
        //会被视图解析器处理
        return "hello";
    }
}

RestFul风格

概念

Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。

功能

资源:互联网所有的事物都可以被抽象为资源
资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。
分别对应 添加、 删除、修改、查询。

代码

新建一个Controller

package com.wangqi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


@Controller
public class RestFulController {

    //@GetMapping只接受GET请求,@PathFVariable注解用于将获取路径变量
    //@RequestMapping(value = "/add/{a}/{b}", method = RequestMethod.GET)
    @GetMapping("/add/{a}/{b}")
    public String test1(@PathVariable int a, @PathVariable int b, Model model) {
        model.addAttribute("msg", "GET请求的结果为:" + (a + b));
        return "test";
    }

    @PostMapping("/add/{a}/{b}")
    public String test2(@PathVariable int a, @PathVariable int b, Model model) {
        model.addAttribute("msg", "GET请求的结果为:" + (a + b));
        return "test";
    }
}

test.jsp

<%--
  Created by IntelliJ IDEA.
  User: 10545
  Date: 2020/8/2
  Time: 18:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

转发和重定向

新建一个Controller

package com.wangqi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ForwardRedirectController {

    @RequestMapping("/t1")
    public String test1(){
        //转发
        return "test";
    }

    @RequestMapping("/t2")
    public String test2(){
        //重定向
        //重定向到index.jsp视图
        return "redirect:/index.jsp";
        //则代表另一个请求
        //return "redirect:t1";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值