springmvc学习笔记.docx

上传人:b****3 文档编号:4419876 上传时间:2022-12-01 格式:DOCX 页数:37 大小:627.94KB
下载 相关 举报
springmvc学习笔记.docx_第1页
第1页 / 共37页
springmvc学习笔记.docx_第2页
第2页 / 共37页
springmvc学习笔记.docx_第3页
第3页 / 共37页
springmvc学习笔记.docx_第4页
第4页 / 共37页
springmvc学习笔记.docx_第5页
第5页 / 共37页
点击查看更多>>
下载资源
资源描述

springmvc学习笔记.docx

《springmvc学习笔记.docx》由会员分享,可在线阅读,更多相关《springmvc学习笔记.docx(37页珍藏版)》请在冰豆网上搜索。

springmvc学习笔记.docx

springmvc学习笔记

[学习笔记]基于注解的spring3.0.xMVC学习笔记

(一)

学习spring3.0.x(以下简称spring3)已经一段日子了,新特性也接触不少,比较感兴趣还是springmvc这一块3.0的mvc变化太大了,跟2.5基本上是两个样子,至于详细的区别可以参考以下文章

领略Spring3.x时代的SpringMVC

spring3mvc变化比较大,但是还是有些2.5的影子的,首先来个2.5也可以使用的注解版本的mvc入门例子.例子采用maven2管理,所以必须要安装m2eclipse插件或者使用maven2进行管理.

本文基于eclipse3.5Galileo-sr2Javaee版本跟m2eclipse插件进行管理.

m2eclipse在线安装地址如下:

http:

//m2eclipse.sonatype.org/sites/m2e

如何安装请参考

Eclipse中安装SpringSourceToolsSuite插件

使用eclipse创建一个mavenproject,选择webappJavaee5.0的模板项目或者simpleproject,对于如何创建可以参考

[使用心得]maven2之m2eclipse使用手册之三第一个SimpleMavenProject与Pom.xml配置说明

[使用心得]maven2之m2eclipse使用手册之六使用Maven2插件创建一个简单的SSH2项目之jetty篇

(一)

对于pom.xml如下:

org.mortbay.jetty

maven-jetty-plugin

6.1.24

org.apache.maven.plugins

maven-compiler-plugin

2.3.1

1.6

1.6

utf-8

org.springframework

spring-webmvc

3.0.3.RELEASE

jar

compile

org.springframework

spring-context

3.0.3.RELEASE

jar

compile

javax.servlet

servlet-api

2.5

jar

provided

javax.servlet.jsp

jsp-api

2.1

jar

provided

--Logging-->

org.slf4j

slf4j-api

${org.slf4j.version}

org.slf4j

jcl-over-slf4j

${org.slf4j.version}

runtime

org.slf4j

slf4j-log4j12

${org.slf4j.version}

runtime

log4j

log4j

1.2.15

javax.mail

mail

javax.jms

jms

com.sun.jdmk

jmxtools

com.sun.jmx

jmxri

runtime

junit

junit

4.8.1

jar

test

1.5.10

spring的mvc本身就是一个servlet,所以在web.xml加入以下内容

StudySpringMVC

org.springframework.web.servlet.DispatcherServlet

springmvc.xml

contextConfigLocation

/WEB-INF/spring/*.xml

1

StudySpringMVC

/

 

其中contextConfigLocation是读取spring的配置文件进行解释,

springmvc.xml如下:

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

xmlns:

xsi="http:

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

aop="http:

//www.springframework.org/schema/aop"

xmlns:

context="http:

//www.springframework.org/schema/context"

xmlns:

mvc="http:

//www.springframework.org/schema/mvc"xmlns:

tx="http:

//www.springframework.org/schema/tx"

xsi:

schemaLocation="http:

//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

http:

//www.springframework.org/schema/contexthttp:

//www.springframework.org/schema/context/spring-context-3.0.xsd

http:

//www.springframework.org/schema/mvchttp:

//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http:

//www.springframework.org/schema/txhttp:

//www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 

--对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">

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(

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

当前位置:首页 > 工程科技 > 城乡园林规划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1