SpringMVC组件解析
【摘要】
文章目录
一、SpringMVC执行流程二、SpringMVC注解解析1.value2.method3.params4.案例
三、Controller组件扫描四、视图解析器五、知识要点
...
一、SpringMVC执行流程
二、SpringMVC注解解析
1.value
注意:value通常可以省略。
@RequestMapping(value = "/quick")
- 1
2.method
@RequestMapping(value = "/quick",method = RequestMethod.GET)
- 1
3.params
@RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"userName"})
- 1
4.案例
结合:Spring入门案例:详细代码
只需要修改:
UserControll:
package com.study.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"userName"})
public String save(){
System.out.println("UserController....");
return "/jsp/success.jsp";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<!-- 配置SpringMVC前端控制器-->
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
测试:
报错:
解决办法:
输入?userName=xxx
三、Controller组件扫描
<!--Controller的组件扫描-->
<context:component-scan base-package="com.study.controller"></context:component-scan>
- 1
- 2
四、视图解析器
spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:http="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<!--Controller的组件扫描-->
<context:component-scan base-package="com.study.controller"></context:component-scan>
<!--配置内部资源视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
UserController
package com.study.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"userName"})
public String save(){
System.out.println("UserController....");
return "success";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
测试:
五、知识要点
文章来源: blog.csdn.net,作者:不会压弯的小飞侠,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_43514330/article/details/125468422
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)