我现在正在为新项目学习 Spring MVC。在那里我遇到了 @RequestMapping
注释,用于了解如何将请求与 URL 映射。
我想了解,在不使用此注释的情况下,请求和 URL 是如何映射的? 它是如何在 XML 文件中配置的?
Controller 代码:
@Controller
public class HelloController {
@RequestMapping(value = "/greeting")
public String sayHello(Model model){
model.addAttribute("greeting","Helloworld");
return "hello";
}
}
servlet-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean name="/greeting.html" class="com.pj.controller.HelloController"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<servlet>
<servlet-name>fitTrackerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>fitTrackerServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
最佳答案
看看这个:Difference between the annotations @GetMapping and @RequestMapping(method = RequestMethod.GET)
现在,
- RequestMapping 用于映射 Controller 的端点。
- 对于 http 动词,有特定的映射注释,例如
- @Getmapping,
- @PostMapping,
- @PutMapping...
关于java - Spring MVC : How url mapping was done before use of @RequestMapping() in spring framework?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59564987/