springmvc学习笔记Word格式.docx
- 文档编号:17345251
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:37
- 大小:627.94KB
springmvc学习笔记Word格式.docx
《springmvc学习笔记Word格式.docx》由会员分享,可在线阅读,更多相关《springmvc学习笔记Word格式.docx(37页珍藏版)》请在冰豆网上搜索。
version>
6.1.24<
/version>
/plugin>
org.apache.maven.plugins<
maven-compiler-plugin<
2.3.1<
configuration>
<
source>
1.6<
/source>
target>
/target>
encoding>
utf-8<
/encoding>
/configuration>
/plugins>
<
/build>
dependencies>
dependency>
org.springframework<
spring-webmvc<
3.0.3.RELEASE<
type>
jar<
/type>
scope>
compile<
/scope>
/dependency>
spring-context<
javax.servlet<
servlet-api<
2.5<
provided<
javax.servlet.jsp<
jsp-api<
2.1<
!
--Logging-->
org.slf4j<
slf4j-api<
${org.slf4j.version}<
jcl-over-slf4j<
runtime<
slf4j-log4j12<
log4j<
1.2.15<
exclusions>
exclusion>
javax.mail<
mail<
/exclusion>
javax.jms<
jms<
com.sun.jdmk<
jmxtools<
com.sun.jmx<
jmxri<
/exclusions>
junit<
4.8.1<
test<
/dependencies>
properties>
org.slf4j.version>
1.5.10<
/org.slf4j.version>
/properties>
spring的mvc本身就是一个servlet,所以在web.xml加入以下内容
servlet>
servlet-name>
StudySpringMVC<
/servlet-name>
servlet-class>
org.springframework.web.servlet.DispatcherServlet<
/servlet-class>
init-param>
description>
springmvc.xml<
/description>
param-name>
contextConfigLocation<
/param-name>
param-value>
/WEB-INF/spring/*.xml<
/param-value>
/init-param>
load-on-startup>
1<
/load-on-startup>
/servlet>
servlet-mapping>
url-pattern>
/<
/url-pattern>
/servlet-mapping>
其中contextConfigLocation是读取spring的配置文件进行解释,
springmvc.xml如下:
beansxmlns="
//www.springframework.org/schema/beans"
xmlns:
xsi="
//www.w3.org/2001/XMLSchema-instance"
aop="
//www.springframework.org/schema/aop"
context="
//www.springframework.org/schema/context"
mvc="
//www.springframework.org/schema/mvc"
tx="
//www.springframework.org/schema/tx"
xsi:
schemaLocation="
//www.springframework.org/schema/beanshttp:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/aophttp:
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
//www.springframework.org/schema/contexthttp:
//www.springframework.org/schema/context/spring-context-3.0.xsd
//www.springframework.org/schema/mvchttp:
//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
//www.springframework.org/schema/txhttp:
//www.springframework.org/schema/tx/spring-tx-3.0.xsd"
>
--对springorg.lxh包下所有注解扫描-->
context:
component-scanbase-package="
org.lxh"
/context:
component-scan>
--支持springmvc新的注解类型详细spring3.0手册15.12.1mvc:
annotation-driven-->
mvc:
annotation-driven/>
--对模型视图名称的解析,即在模型视图名称添加前后缀,在requestmapping输入的地址后自动调用该类进行视图解析-->
beanid="
viewResolver"
class="
org.springframework.web.servlet.view.InternalResourceViewResolver"
propertyname="
viewClass"
value="
org.springframework.web.servlet.view.JstlView"
/>
prefix"
value="
/WEB-INF/jsp/"
suffix"
.jsp"
/property>
/bean>
/beans>
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已经正常执行了
输入以下地址:
//localhost:
8080/StudySpringMVC/welcome.do
看到如下图的内容:
注意:
调用该方法的时候会自动搜索beanid="
的设置,然后查找该墓里下的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
/"
如果需要使用jstl的话则需要在<
property>
标签中加入
prefix:
则为前缀,也就是目录的地址,一般以"
WEB-INF/xx"
为主,若为"
则为全局使用
suffix:
则为后缀,也就是文件名的后缀.使用InternalResourceViewResolver类是只支持jsp,不支持html等其他后缀,如果强制加入其他后缀的话胡出现死循环,至于其他视图的话则以后在使用的时候再介绍
web.xml
ThisisSpringMVCDispatcherServlet<
SpringMVCDispatchServlet<
SpringContext<
classpath*:
配置spring的DispatcherServlet的时候,如果需要使用自定义名字的spring配置文件的话,需要在<
serlvet>
中加入<
这个参数,否则的话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;
@RequestMapping("
/test"
)
publicclassHelloWordController{
privateLoggerlogger=LoggerFactory.getLogger(HelloWordController.class);
@RequestMapping("
/hello"
publicvoidHello(){
logger.info(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- springmvc 学习 笔记