SpringMVC的@Controller、@RequestMapping、@ResponseBody等注解,如何正确使用?
- 内容介绍
- 文章标签
- 相关推荐
本文共计526个文字,预计阅读时间需要3分钟。
`@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配置文件
contextConfigLocation
classpath:springmvc.xml
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="ISO-8859-1"%>
testParamAndHeader
testAntPath
testPathVariable Test Get 返回的成功页面success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="ISO-8859-1"%>
Success
ModelAndView: ${requestScope.studentMsg } Map: ${requestScope.users } Map: ${requestScope.xixi } SessionAttributes: ${sessionScope.user } SessionAttributes: ${sessionScope.msg }本文共计526个文字,预计阅读时间需要3分钟。
`@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配置文件
contextConfigLocation
classpath:springmvc.xml
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="ISO-8859-1"%>
testParamAndHeader
testAntPath
testPathVariable Test Get 返回的成功页面success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="ISO-8859-1"%>

