SpringMVC的@Controller、@RequestMapping、@ResponseBody等注解,如何正确使用?

2026-04-15 08:238阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计526个文字,预计阅读时间需要3分钟。

SpringMVC的@Controller、@RequestMapping、@ResponseBody等注解,如何正确使用?

`@Controller` 将目标类标识为请求处理器,`@RequestMapping` 映射请求路径到处理方法。以下是对代码段的简化:

java@Controllerpublic class HelloWorld { private String s;}

@Controller将目标类标识为一个请求处理器,@RequestMapping映射请求

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorld { private static String success="success"; @RequestMapping("/helloworld") public String hello() { System.out.println("Hello World"); return success; } } web.xml配置文件

springDispatcherServlet org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml 1 springDispatcherServlet / HiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter HiddenHttpMethodFilter /* springmvc.xml配置文件

请求页面index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="ISO-8859-1"%> Insert title here Hello World
testParamAndHeader
testAntPath
testPathVariable

Test Get
返回的成功页面success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="ISO-8859-1"%> Insert title here

Success

ModelAndView: ${requestScope.studentMsg } Map: ${requestScope.users } Map: ${requestScope.xixi } SessionAttributes: ${sessionScope.user } SessionAttributes: ${sessionScope.msg }

SpringMVC的@Controller、@RequestMapping、@ResponseBody等注解,如何正确使用?

本文共计526个文字,预计阅读时间需要3分钟。

SpringMVC的@Controller、@RequestMapping、@ResponseBody等注解,如何正确使用?

`@Controller` 将目标类标识为请求处理器,`@RequestMapping` 映射请求路径到处理方法。以下是对代码段的简化:

java@Controllerpublic class HelloWorld { private String s;}

@Controller将目标类标识为一个请求处理器,@RequestMapping映射请求

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorld { private static String success="success"; @RequestMapping("/helloworld") public String hello() { System.out.println("Hello World"); return success; } } web.xml配置文件

springDispatcherServlet org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml 1 springDispatcherServlet / HiddenHttpMethodFilter org.springframework.web.filter.HiddenHttpMethodFilter HiddenHttpMethodFilter /* springmvc.xml配置文件

请求页面index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="ISO-8859-1"%> Insert title here Hello World
testParamAndHeader
testAntPath
testPathVariable

Test Get
返回的成功页面success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="ISO-8859-1"%> Insert title here

Success

ModelAndView: ${requestScope.studentMsg } Map: ${requestScope.users } Map: ${requestScope.xixi } SessionAttributes: ${sessionScope.user } SessionAttributes: ${sessionScope.msg }

SpringMVC的@Controller、@RequestMapping、@ResponseBody等注解,如何正确使用?