开发者学堂课程【SpringMVC 框架入门:使用注解开发】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/22/detail/452
使用注解开发
内容介绍:
1. 导入jar包
2. Web.xml
3. Controller
4. Spring mvc的配置
l 导入jar包
commons-logging-1.1.1.jar
spring-aop-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
l Web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.Dispatcher Servlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
注意:红色配置指明mvc的配置文件位置(src/mvc.xml)
l Controller
@Controller
public class HelloController {
@RequestMapping("/hello")
public ModelAndView hello(HttpServletRequest reg, HttpServletResponse resp){
ModelAndView mv= new ModelAndView();
//封装要显示到视图中的数据
mv. addObject ("msg"," hello annotation");
//视图名
my. setViewName ("hello");// web-inf/ isp/ hello. isp
return mv;
}
}
l Spring mvc的配置
<?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:p="http://ww.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置渲染器 视图解析器
-->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternaLResource ViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.Jstlview"/>
<!--结果视图的前缀
-->
<property name="prefix" value="/WEB-INF/isp/"/>
<!--结果视图的后缀
-->
<property name="suffix" value=".jsp"/>
</bean>
<一扫描该包下的注解
-->
< context: component- scan
base-package="cn.sxt.controller"/>
</ beans >