【正文】
jsp //bean要使用注解的SpringMVC需要在SpringMVC的配置文件中進(jìn)行聲明,具體方式為先引入mvc命名空間,然后利用mvc:annotationdriven /進(jìn)行聲明。beans xmlns= xmlns:context= xmlns:mvc= xmlns:p= xmlns:xsi= xmlns:websocket= xsi:schemaLocation= context:ponentscan basepackage= usedefaultfilters=falsecontext:includefilter expression= type=annotation//context:ponentscan mvc:annotationdriven conversionservice=conversionService mvc:asyncsupport defaulttimeout=6000/mvc:asyncsupport /mvc:annotationdriven/beans在SpringMVC中Controller不需要繼承什么類,也不需要實(shí)現(xiàn)什么接口,一切使用了@Controller進(jìn)行標(biāo)記的類都是Controller。@Controllerpublic class UserController {}有了Controller之后,那么到底是怎樣請(qǐng)求一個(gè)Controller具體的方法的呢,那是通過(guò)@RequestMapping來(lái)標(biāo)記 的,@RequestMapping可以標(biāo)記在類上面,也可以標(biāo)記在方法上,當(dāng)方法上和類上都標(biāo)記了@RequestMapping的時(shí)候,那么對(duì)應(yīng)的方 法對(duì)應(yīng)的Url就是類上的加方法上的,如下面的index方法,其對(duì)應(yīng)的URL應(yīng)為類上的/blog加上index方法上的/index,所以應(yīng)為 /blog/index,所以當(dāng)請(qǐng)求/blog/。加在類上的 @RequestMapping不是必須的,當(dāng)Controller類上加上了@RequestMapping的時(shí)候,那么Controller方法上的 @RequestMapping就是相對(duì)于類上的@RequestMapping而言的,也就是前面說(shuō)的請(qǐng)求映射的時(shí)候是類上的地址加方法上的地址,而當(dāng) Controller類上沒(méi)有加@RequestMapping的時(shí)候,方法上的@RequestMapping就是絕對(duì)路徑了。@Controllerpublic class UserController { @RequestMapping(/users/all) public ListUser findAll() { return ()。 }}在Controller的方法中,如果需要WEB元素HttpServletRequest,HttpSession和HttpServletResponse ,只需要在給方法一個(gè)對(duì)應(yīng)的參數(shù),那么在訪問(wèn)的時(shí)候SpringMVC就會(huì)自動(dòng)給其傳值,但是需要注意的是在傳入Session的 時(shí)候如果是第一次訪問(wèn)系統(tǒng)的時(shí)候就調(diào)用session會(huì)報(bào)錯(cuò),因?yàn)檫@個(gè)時(shí)候session還沒(méi)有生成。附錄B 英文原文Spring MVC Brief Introduction Based on AnnotationsSpring MVC is a MVC framework based on the dispatcherservlet. Each request first visit is dispatcherservlet, dispatcherservlet responsible for forwarding a request to the appropriate handler, after the handler back again the corresponding view (view) and model (model), returned to the view and the model can not specified, can only return the model or only return a view or do not return. In the spring MVC uses annotations and handler processor is based on requestmapping and @Controller the two annotations. @Controller declare a handler class, requestmapping statement corresponding to request the mapping relationship, this is can provide a very flexible matching and processing.Dispatcherservlet is inherited from the ervlet, since the spring MVC is dispatcherservlet. Well then, let39。s configure your dispatcherservlet based on, let it to manage our hope it management. HttpServlet is declared in the file..servlet servletnamesunjingServlet/servletname servletclass/servletclass loadonstartup1/loadonstartup /servlet servletmapping servletnamesunjingServlet/servletname urlpattern//urlpattern /servletmapping/servletThe above statement of a sunjingServlet DispatcherServlet, the Servlet will handle all requests. When initializing the dispatcherservlet, springmvc default to / WEBINF directory looking for a call [servletname] configuration file, to initialize the bean object. The file corresponding to the bean object will cover stated in the spring configuration file of the same name bean object. Such as the above will be in the / WEBINF directory for a file called 。 of course, can also be in the servlet configuration file to statement position, that is by the initialization parameter to the servlet to set contextconfiglocation parameter values. servlet servletnamesunjingServlet/servletname servletclass/servletclass initparam paramnamecontextConfigLocation/paramname paramvalueWEBINF/spring//paramvalue /initparam loadonstartup1/loadonstartup asyncsupportedtrue/asyncsupported multipartconfig maxfilesize50000/maxfilesize /multipartconfig /servlet servletmapping servletnamesunjingServlet/servletname urlpattern//urlpattern /servletmapping/servletDispatcherServlet will handle the Request request and generate the corresponding view and return to use some special bean.About the view of return, controller only is responsible for passing back a value, then in the end return is what view, is controlled by the view resolvers, in JSP mon view resolver is InternalResourceViewResovler it would require a prefix and a suffix. bean class= property name=prefix value=/WEBINF/ / property name=suffix value=.jsp / /beanTo use annotated springmvc need to declare in springmvc configuration file, the specific way to first introduce the MVC namespace, then the mvc:annotationdriven / declarations.beans xmlns= xmlns:context= xmlns:mvc= xmlns:p= xmlns:xsi= xmlns:websocket= xsi:schemaLocation= context:ponentscan basepackage= usedefaultfilters=falsecontext:includefilter expression= type=annotation//context:ponentscan mvc:annotationdriven conversionservice=conversionService mvc:asyncsupport defaulttimeout=6000/mvc:asyncsupport /mvc:annotationdriven/beansIn the spring MVC controller does not need what kind of inheritance, also does not need to implement any interface, the use of all the @Controller class of markers are controller.@Controllerpublic class UserController {}Controller, so in the end is how to request a specific controller method, that is through requestmapping to mark that requestmapping can be labeled in the top class can also be marked on the method, when the method and class are labeled requestmapping, then corresponding method corresponding to the URL is the class and method, index methods such as the following, the corresponding URL should /index class / blog and index method, so as /blog/index, so when the request /blog/ will visit our blogcontroller index method. Requestmapping in class is not necessary, when the controller class added requestmapping, then controller method on requestmapping is relative to the class on requestmapping is said in front of the request mappings