springweb30MVC注解附实例.docx

上传人:b****1 文档编号:2568951 上传时间:2023-05-04 格式:DOCX 页数:53 大小:74.95KB
下载 相关 举报
springweb30MVC注解附实例.docx_第1页
第1页 / 共53页
springweb30MVC注解附实例.docx_第2页
第2页 / 共53页
springweb30MVC注解附实例.docx_第3页
第3页 / 共53页
springweb30MVC注解附实例.docx_第4页
第4页 / 共53页
springweb30MVC注解附实例.docx_第5页
第5页 / 共53页
springweb30MVC注解附实例.docx_第6页
第6页 / 共53页
springweb30MVC注解附实例.docx_第7页
第7页 / 共53页
springweb30MVC注解附实例.docx_第8页
第8页 / 共53页
springweb30MVC注解附实例.docx_第9页
第9页 / 共53页
springweb30MVC注解附实例.docx_第10页
第10页 / 共53页
springweb30MVC注解附实例.docx_第11页
第11页 / 共53页
springweb30MVC注解附实例.docx_第12页
第12页 / 共53页
springweb30MVC注解附实例.docx_第13页
第13页 / 共53页
springweb30MVC注解附实例.docx_第14页
第14页 / 共53页
springweb30MVC注解附实例.docx_第15页
第15页 / 共53页
springweb30MVC注解附实例.docx_第16页
第16页 / 共53页
springweb30MVC注解附实例.docx_第17页
第17页 / 共53页
springweb30MVC注解附实例.docx_第18页
第18页 / 共53页
springweb30MVC注解附实例.docx_第19页
第19页 / 共53页
springweb30MVC注解附实例.docx_第20页
第20页 / 共53页
亲,该文档总共53页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

springweb30MVC注解附实例.docx

《springweb30MVC注解附实例.docx》由会员分享,可在线阅读,更多相关《springweb30MVC注解附实例.docx(53页珍藏版)》请在冰点文库上搜索。

springweb30MVC注解附实例.docx

springweb30MVC注解附实例

SPRINGWEBMVC注解实例说明

0.提示

1)Spring发行版本附带了PetClinic示例,它是一个在简单的表单处理的上下文中,利用了本节中说明的注解支持的Web应用程序。

可以在“samples/petclinic”目录中找到PetClinic应用程序。

2)另外一个建立在基于注解的WebMVC上的示例应用程序,请见imagedb。

这个示例集中在无状态的multi-action控制器,包括多段文件上传的处理。

可以在“samples/imagedb”目录找到imagedb应用程序。

1.建立dispatcher实现注解支持

只有对应的HandlerMapping(为了实现类型级别的注解)和/或HandlerAdapter(为了实现方法级别的注解)出现在dispatcher中时,@RequestMapping才会被处理。

这在DispatcherServlet和DispatcherPortlet中都是缺省的行为。

然而,如果是在定义自己的HandlerMappings或HandlerAdapters,就需要确保一个对应的自定义的DefaultAnnotationHandlerMapping和/或AnnotationMethodHandlerAdapter同样被定义——假设想要使用@RequestMapping。

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xsi:

schemaLocation="http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-2.5.xsd">

DefaultAnnotationHandlerMapping"/>

AnnotationMethodHandlerAdapter"/>

...(controllerbeandefinitions)...

例1:

雁联zfpt-servlet.xml

配置DefaultAnnotationHandlerMapping和/或AnnotationMethodHandlerAdapter

component-scanbase-package="com.ylink.zfpt.web.spring"/>

DefaultAnnotationHandlerMapping">

1

AnnotationMethodHandlerAdapter">

class="com.ylink.zfpt.web.intercepor.SuperUserAccessInterceptor">

例2:

web.xml

xmlversion="1.0"encoding="ISO-8859-1"?

>

xmlns=xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xsi:

schemaLocation="

SpringPetClinic

SpringPetClinicsampleapplication

2.1webAppRootKey

--

Keyofthesystempropertythatshouldspecifytherootdirectoryofthis

webapp.AppliedbyWebAppRootListenerorLog4jConfigListener.

-->

webAppRootKey

petclinic.root

2.3log4jConfigLocation

--

LocationoftheLog4Jconfigfile,forinitializationandrefreshchecks.

AppliedbyLog4jConfigListener.

-->

log4jConfigLocation

/WEB-INF/classes/log4j.properties

2.4contextConfigLocation

--

-LocationoftheXMLfilethatdefinestherootapplicationcontext.

-AppliedbyContextLoaderServlet.

-

-Canbesetto:

-"/WEB-INF/applicationContext-hibernate.xml"fortheHibernateimplementation,

-"/WEB-INF/applicationContext-jpa.xml"fortheJPAone,or

-"/WEB-INF/applicationContext-jdbc.xml"fortheJDBCone.

-->

contextConfigLocation

/WEB-INF/spring/applicationContext-jdbc.xml

/WEB-INF/applicationContext-security.xml

--

/WEB-INF/spring/applicationContext-hibernate.xml

/WEB-INF/spring/applicationContext-jpa.xml

-->

--

TousetheJPAvariantabove,youwillneedtoenableSpringload-time

weavinginyourserverenvironment.Outofthebox,Springwilltryto

detecttherunningenvironmentandusetheappropriateweaverbutifthat

fails,onemustenableonebyhandorusetheVM-wideweaver.

SeePetClinic'sreadmeand/orSpring'sJPAdocumentationformoreinformation.

-->

2.5springSecurityFilterChain

springSecurityFilterChain

org.springframework.web.filter.DelegatingFilterProxy

springSecurityFilterChain

/*

2.6Log4jConfigListener

--

-ConfiguresLog4Jforthiswebapp.

-Asthiscontextspecifiesacontext-param"log4jConfigLocation",itsfilepath

-isusedtoloadtheLog4Jconfiguration,includingperiodicrefreshchecks.

-

-WouldfallbacktodefaultLog4Jinitialization(non-refreshing)ifnospecial

-context-paramsaregiven.

-

-Exportsa"webapprootkey",i.e.asystempropertythatspecifiestheroot

-directoryofthiswebapp,forusageinlogfilepaths.

-Thiswebappspecifies"petclinic.root"(seelog4j.propertiesfile).

-->

--Leavethelistenercommented-outifusingJBoss-->

org.springframework.web.util.Log4jConfigListener

2.7ContextLoaderListener

--

-Loadstherootapplicationcontextofthiswebappatstartup,

-bydefaultfrom"/WEB-INF/applicationContext.xml".

-NotethatyouneedtofallbacktoSpring'sContextLoaderServletfor

-J2EEserversthatdonotfollowtheServlet2.4initializationorder.

-

-UseWebApplicationContextUtils.getWebApplicationContext(servletContext)

-toaccessitanywhereinthewebapplication,outsideoftheframework.

-

-Therootcontextistheparentofallservlet-specificcontexts.

-Thismeansthatitsbeansareautomaticallyavailableinthesechildcontexts,

-bothforgetBean(name)callsand(external)beanreferences.

-->

org.springframework.web.context.ContextLoaderListener

2.8DispatcherServlet

--

-Servletthatdispatchesrequesttoregisteredhandlers(Controllerimplementations).

-Hasitsownapplicationcontext,bydefaultdefinedin"{servlet-name}-servlet.xml",

-i.e."petclinic-servlet.xml".

-

-Awebappcancontainanynumberofsuchservlets.

-Notethatthiswebapphasasharedrootapplicationcontext,servingasparent

-ofallDispatcherServletcontexts.

-->

petclinic

org.springframework.web.servlet.DispatcherServlet

2

--

-Mapsthepetclinicdispatcherto"*.do".Allhandlermappingsin

-petclinic-servlet.xmlwillbydefaultbeappliedtothissubpath.

-Ifamappingisn'ta/*subpath,thehandlermappingsareconsidered

-relativetothewebapproot.

-

-NOTE:

Asingledispatchercanbemappedtomultiplepaths,likeanyservlet.

-->

petclinic

/

2.9exception.java

java.lang.Exception

--Displaysastacktrace-->

/WEB-INF/jsp/uncaughtException.jsp

 

例3:

雁联web.xml

如果你想要自定义映射策略,显式的定义一个DefaultAnnotationHandlerMapping和/或AnnotationMethodHandlerAdapter也有实际意义。

例如,指定一个自定义的PathMatcher或者WebBindingInitializer

1.一个简单的基于注解的Controller

使用过低版本SpringMVC的读者都知道:

1.当创建一个Controller时,我们需要直接或间接地实现org.springframework.web.servlet.mvc.Controller接口。

一般情况下,我们是通过继承SimpleFormController或MultiActionController来定义自己的Controller的。

2.在定义Controller后,一个重要的事件是在SpringMVC的配置文件中通过HandlerMapping定义请求和控制器的映射关系,以便将两者关联起来。

3.来看一下基于注解的Controller是如何定义做到这一点的,下面是使用注解的BbtForumController:

实现效果:

启动Tomcat,发送http:

//localhost/forum.doURL请求,BbtForumController的listAllBoard()方法将响应这个请求,并转向WEB-INF/jsp/listBoard.jsp的视图页面。

清单1.BbtForumController.java

packagecom.baobaotao.web;

importcom.baobaotao.service.BbtForumService;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.ModelAttribute;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.RequestMethod;

importjava.util.Collection;

@Controller//<——①

@RequestMapping("/forum.do")

publicclassBbtForumController{

@Autowired

privateBbtForumServicebbtForumService;

@RequestMapping//<——②

publicStringlistAllBoard(){

bbtForumService.getAllBoard();

System.out.println("calllistAllBoardmethod.");

return"listBoard";

}

}

在①处使用了两个注解,分别是@Controller和@RequestMapping。

在“使用Spring2.5基于注解驱动的IoC”这篇文章里,笔者曾经指出过@Controller、@Service以及@Repository和@Component注解的作用是等价的:

将一个类成为Spring容器的Bean。

由于SpringMVC的Controller必须事先是一个Bean,所以@Controller注解是不可缺少的。

真正让BbtForumController具备SpringMVCController功能的是@RequestMapping这个注解。

@RequestMapping可以标注在类定义处,将Controller和特定请求关联起来;还可以标注在方法签名处,以便进一步对请求进行分流。

在①处,我们让BbtForumController关联“/forum.do”的请求,而②处,我们具体地指定listAllBoard()方法来处理请求。

所以在类声明处标注的@RequestMapping相当于让POJO实现了Controller接口,而在方法定义处的@RequestMapping相当于让POJO扩展Spring预定义的Controller(如SimpleFormController等)。

清单2.web.xml:

启用Spring容器和SpringMVC框架

为了让基于注解的SpringMVC真正工作起来,需要在SpringMVC对应的xxx-servlet.xml配置文件中做一些手脚。

在此之前,还是先来看一下web.xml的配置吧

xmlversion="1.0"encoding="UTF-8"?

>

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xsi:

schemaLocation="

version="2.5">

SpringAnnotationMVCSample

--Spring服务层的配置文件-->

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2