value="org.springframework.web.servlet.view.JstlView"/>
UserController.Java类:
@Controller
publicclassUserController{
privateLoggerlogger=org.slf4j.LoggerFactory
.getLogger(UserController.class);
@RequestMapping(value="/welcome.do",method=RequestMethod.GET)
publicvoidwelcome(){
logger.info("Welcome!
");
System.out.println("helloworld");
}
}
@Controller:
将一个类成为Spring容器的Bean
@RequestMapping:
指定该方法处理的URL请求,当在浏览器中输入该url地址的时候则会执行会执行指定的方法.
-----value:
要运行的url地址
-----method:
该方法在什么才执行.默认GET
运行jetty插件命令jetty:
run
看到如下图:
看到该信息后则证明spring的mvc已经正常执行了
输入以下地址:
http:
//localhost:
8080/StudySpringMVC/welcome.do
看到如下图的内容:
注意:
调用该方法的时候会自动搜索beanid="viewResolver"的设置,然后查找该墓里下的jsp名字构成是所调用的方法名+.jsp,如果没有的话会出现后台NOT_FOUND的提示信息.
[学习笔记]基于注解的spring3.0.xMVC学习笔记
(二)
用惯了maven管理项目之后会发现自己懒惰了很多,所以决定放弃使用maven去学习spring3的mvc,采用传统的webproject进行学习,好了闲话不说,首先我们需要知道springmvc需要加什么包.
使用到springmvc的需要加入以下依赖包:
org.springframework.aop-3.0.3.RELEASE.jar--------------Spring的切面编程
org.springframework.asm-3.0.3.RELEASE.jar--------------Spring独立的asm程序
org.springframework.beans-3.0.3.RELEASE.jar------------SpringIoC(依赖注入)的基础实现
org.springframework.context-3.0.3.RELEASE.jar-----------Spring提供在基础IoC功能上的扩展服务
org.springframework.core-3.0.3.RELEASE.jar--------------Spring的核心包
org.springframework.expression-3.0.3.RELEASE.jar------Spring表达式语言
org.springframework.web-3.0.3.RELEASE.jar--------------SpringWeb下的工具包
org.springframework.web.servlet-3.0.3.RELEASE.jar-----SpringMVC工具包,并且对JEE6.0Servlet3.0的支持
除了spring的包之外还需要加入以下几个依赖包:
com.springsource.org.aopalliance-1.0.0.jar---------------aop的工具包
mons.logging-1.1.1.jar----------commons的日志管理
本文使用了slf4j日志管理,所以需要加入以下包
slf4j-api-1.5.10.jar--------------------------------------------日志管理的增强
slf4j-log4j12-1.5.10.jar---------------------------------------日志包的连接层
所有包如图1springmvc需要的包:
图1:
springmvc需要的包
实现的例子还是以helloworld的简单输出,以后再逐步加入数据库的操作,新建一个resource的sourcefolder,用来存放spring的配置文件跟log4j.xml的日志文件
使用日志管理就必须要加入log4j.xml或者是log4j.properties进行配置,这里采用了Log4j.mxl进行配置
Log4j.xml:
springmvc.xml
--对springorg.lxh包下所有注解扫描-->
component-scanbase-package="org.lxh">
component-scan>
--支持springmvc新的注解类型详细spring3.0手册15.12.1mvc:
annotation-driven-->
annotation-driven/>
--对模型视图名称的解析,即在模型视图名称添加前后缀,在requestmapping输入的地址后自动调用该类进行视图解析-->
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
如果需要使用jstl的话则需要在标签中加入
prefix:
则为前缀,也就是目录的地址,一般以"WEB-INF/xx"为主,若为"/"则为全局使用
suffix:
则为后缀,也就是文件名的后缀.使用InternalResourceViewResolver类是只支持jsp,不支持html等其他后缀,如果强制加入其他后缀的话胡出现死循环,至于其他视图的话则以后在使用的时候再介绍
web.xml
ThisisSpringMVCDispatcherServlet
SpringMVCDispatchServlet
org.springframework.web.servlet.DispatcherServlet
SpringContext
contextConfigLocation
classpath*:
springmvc.xml
1
SpringMVCDispatchServlet
/
注意:
配置spring的DispatcherServlet的时候,如果需要使用自定义名字的spring配置文件的话,需要在中加入这个参数,否则的话spring会默认查找web-inf/classes下面以[servlet-name]-servlet.xml的文件,会出现以下错误:
ERROR:
org.springframework.web.servlet.DispatcherServlet-Contextinitializationfailed
org.springframework.beans.factory.BeanDefinitionStoreException:
IOExceptionparsingXMLdocumentfromServletContextresource[/WEB-INF/SpringMVCDispatchServlet-servlet.xml];nestedexceptionisjava.io.FileNotFoundException:
CouldnotopenServletContextresource[/WEB-INF/SpringMVCDispatchServlet-servlet.xml]
HelloWordController.java:
packageorg.lxh.mvc.controller;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/test")
publicclassHelloWordController{
privateLoggerlogger=LoggerFactory.getLogger(HelloWordController.class);
@RequestMapping("/hello")
publicvoidHello(){
logger.info(