Java控制器controller_第一个 Controller 控制器

```

package com.funtl.my.shop.web.controller;

import com.funtl.my.shop.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

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

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

@Controller

public class LoginController {

@Autowired

private UserService userService;

@RequestMapping(value = {"", "login"}, method = RequestMethod.GET)

public String login() {

return "login";

}

@RequestMapping(value = "login", method = RequestMethod.POST)

public String login(@RequestParam(required = true) String email, @RequestParam(required = true) String password) {

return "redirect:/main";

}

}

```

注解说明

* @Controller

在 Spring MVC 中,控制器 Controller 负责处理由 DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个 Model ,然后再把该 Model 返回给对应的 View 进行展示。在 Spring MVC 中提供了一个非常简便的定义 Controller 的方法,你无需继承特定的类或实现特定的接口,只需使用 @Controller 标记一个类是 Controller ,然后使用 @RequestMapping 和 @RequestParam 等一些注解用以定义 URL 请求和 Controller 方法之间的映射,这样的 Controller 就能被外界访问到。此外 Controller 不会直接依赖于 HttpServletRequest 和 HttpServletResponse 等 HttpServlet 对象,它们可以通过 Controller 的方法参数灵活的获取到。

* @Controller 用于标记在一个类上,使用它标记的类就是一个 Spring MVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了 @RequestMapping 注解。@Controller 只是定义了一个控制器类,而使用 @RequestMapping 注解的方法才是真正处理请求的处理器。

* @RequestMapping

RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

RequestMapping 注解有六个属性:

* value, method

value:指定请求的实际地址,指定的地址可以是 URI Template 模式

method:指定请求的method类型, GET、POST、PUT、DELETE 等

* consumes,produces

consumes:指定处理请求的提交内容类型(Content-Type),例如 application/json, text/html

produces: 指定返回的内容类型,仅当 request 请求头中的(Accept)类型中包含该指定类型才返回

params,headers

* params:指定 request 中必须包含某些参数值是,才让该方法处理

* headers:指定 request 中必须包含某些指定的 header 值,才能让该方法处理请求

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Java控制器Controller)的示例代码,其中包含两个不同的控制器方法,一个方法有参数,控制器方法类型分别为GET和POST。 ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller @RequestMapping("/example") public class ExampleController { @GetMapping("/hello") @ResponseBody public String helloWorld() { return "Hello, World!"; } @PostMapping("/greet/{name}") @ResponseBody public String greetName(@PathVariable String name, @RequestParam(required = false) String greeting) { if (greeting != null) { return greeting + ", " + name + "!"; } else { return "Hello, " + name + "!"; } } } ``` 在上面的代码中,我们首先使用`@Controller`注解告诉Spring框架这是一个控制器。然后使用`@RequestMapping`注解指定了该控制器的基本URL路径为`/example`。 接着,我们定义了两个不同的控制器方法。第一个方法是`helloWorld()`,使用了`@GetMapping`注解表示该方法处理GET请求,并且URL路径为`/example/hello`。该方法返回一个字符串,表示向客户端返回的响应数据。 第二个方法是`greetName()`,使用了`@PostMapping`注解表示该方法处理POST请求,并且URL路径为`/example/greet/{name}`。其中,`{name}`是一个占位符,表示请求路径中的某个参数。该方法同时还接收了一个名为`greeting`的可选参数,使用了`@RequestParam`注解进行声明。该方法根据请求参数返回不同的响应数据。如果请求参数中包含`greeting`参数,则返回使用该参数进行拼接的字符串,否则返回默认的问候语。 需要注意的是,如果要使用上述代码,需要先在Spring框架的配置文件中进行相关的配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值