@Controller注解开发类

本文介绍了Spring MVC中常用的注解@Controller和@RequestMapping的使用方法。@Controller注解标记类为Spring MVC控制器,而@RequestMapping注解用于映射URL到处理方法,实现请求分发。示例代码展示了如何在方法上使用@RequestMapping来处理不同URL请求,并向Model添加属性,返回同一视图名称。
摘要由CSDN通过智能技术生成
package com.kuang.controller;

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

@Controller//代表这个类会被SPring接管
//被这个类注解的类中的所有方法,如果返回值是String,并且有具体的页面可以跳转那么就会被视图解析器解析;
public class ControllerTest2 {

    @RequestMapping("/t2")
  //@RequestMapping注解用于映射url到控制器类或一个特定的处理程序方法。可用于类或者方法上,
  //用于类上,表示类中的所有相应请求的方法都是以该地址作为父路径。
    public String test1(Model model){
        model.addAttribute("msg", "阿里巴巴");
        return "test"; //WEB-INF/jsp/rest.jsp
    }
    @RequestMapping("/t3")
    public String test2(Model model){
        model.addAttribute("msg", "华为");
        return "test";
    }
}

注解开发常用,比较简单。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值