SpringMVC03——Controller 、 RequestMapping、结果跳转

目录

一、Controller

1、编写一个测试类

2、SpringMVC配置文件中注册请求的bean

3、删除处理器映射器、处理器适配器配置

二、使用注解Controller

1、添加注解扫描

2、编写注解实现类

3、测试

三、RequestMapping

四、结果跳转方式

1、ModelAndView

2、ServletAPI

转发

重定向

3、SpringMVC

转发(不带视图解析器)

 重定向(不带视图解析器)

转发和重定向 - 有视图解析器;


一、Controller

1、编写一个测试类

public class TestController implements Controller {

    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mv = new ModelAndView();

        mv.addObject("msg","hello,Controller2");

        mv.setViewName("test");

        return mv;
    }
}

2、SpringMVC配置文件中注册请求的bean

    <bean name="/t" class="com.jia.controller.TestController"/>

3、删除处理器映射器、处理器适配器配置

注销掉

4、测试

  • 实现接口Controller定义控制器是较老的办法
  • 缺点是:一个控制器中只有一个方法,如果要多个方法则需要定义多个Controller;定义的方式比 较麻烦;

二、使用注解Controller

1、添加注解扫描

Spring可以使用扫描机制来找到应用程序中所有基于注解的控制器类

    <!--自动扫描包,让指定包下得注解生效,有IOC同一管理-->
    <context:component-scan base-package="com.jia.controller"/>

2、编写注解实现类

//@Controller注解的类会自动添加到Spring上下文中
@Controller
public class TestController2 {

    //映射访问路径
    @RequestMapping("/c")
    public String test(Model model){
        //Spring MVC会自动实例化一个Model对象用于向视图中传值
        model.addAttribute("msg","hello,注解的Controller");
        //返回视图位置
        return "hello";
    }
}

3、测试

三、RequestMapping

@RequestMapping 注解用于映射 url 到控制器类或一个特定的处理程序方法。可用于类或方法上。
用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
//映射访问路径
    @RequestMapping(value = "/hello")    //匹配所有请求的/hello
------------------------------------------------------------------------
    @RequestMapping(value = "/hello",method = RequestMethod.GET)    //只匹配Get请求的/hello
------------------------------------------------------------------------
    @GetMapping(value = "/hello")        //只匹配Get请求的/hello
------------------------------------------------------------------------
    @PostMapping(value = "/hello")      //只匹配Post请求的/hello
------------------------------------------------------------------------

四、结果跳转方式

1、ModelAndView

  • 设置ModelAndView对象 , 根据view的名称 , 视图解析器跳到指定的页面 .
  • 页面 : {视图解析器前缀} + viewName +{视图解析器后缀}
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
@Controller
public class TestMV {

    @RequestMapping("/mv")
    public ModelAndView test(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","hello,ModelAndView");  //封装数据
        mv.setViewName("hello");//设置跳转
        return mv;
    }
}

2、ServletAPI

通过设置ServletAPI , 不需要视图解析器

转发

@Controller
public class TestServlet {

    @RequestMapping("/s")
    public void test(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setAttribute("msg","Hello,Servlet转发");

        request.getRequestDispatcher("WEB-INF/jsp/hello.jsp").forward(request,response);

    }
}

重定向

(转发不需要加项目名,重定向需要加项目名)

@Controller
public class TestServlet {

    @RequestMapping("/s")
    public void test(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("______________");
        try {
            response.sendRedirect("/springmvctest02_war_exploded/index.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

3、SpringMVC

转发(不带视图解析器)

@Controller
public class TestSpringMVC {
    @RequestMapping("/mvc")
    public String test(HttpServletRequest request){

        request.setAttribute("msg","使用SpringMVC实现转发");

        return "/WEB-INF/jsp/hello.jsp";

    }
}
@Controller
public class TestSpringMVC {
    @RequestMapping("/mvc")
    public String test(HttpServletRequest request){

        request.setAttribute("msg","使用SpringMVC实现转发");

        return "forward:/WEB-INF/jsp/hello.jsp";//显示定义为转发

    }
}

 重定向(不带视图解析器)

@Controller
public class TestSpringMVC {
    @RequestMapping("/mvc")
    public String test(){

        return "redirect:/jsp/hello.jsp";

    }
}

转发和重定向 - 有视图解析器;

@Controller
public class TestSpringMVC {
    @RequestMapping("/mvc")
    public String test(){

        return "test";

    }
    @RequestMapping("/mvc2")
    public String test2(){

        return "redirect:index.jsp";

    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上兵伐眸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值