天天看點

使用注解開發 | 學習筆記

開發者學堂課程【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.xsd http://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 >