博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于注解的spring3.0.x MVC学习笔记(二)
阅读量:4662 次
发布时间:2019-06-09

本文共 4482 字,大约阅读时间需要 14 分钟。

用惯了maven管理项目之后会发现自己懒惰了很多,所以决定放弃使用maven去学习spring3的mvc,采用传统的web project进行学习,好了闲话不说,首先我们需要知道spring mvc需要加什么包.

使用到spring mvc的需要加入以下依赖包:

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.0 Servlet3.0的支持

除了spring的包之外还需要加入以下几个依赖包:

com.springsource.org.aopalliance-1.0.0.jar---------------aop的工具包

com.springsource.org.apache.commons.logging-1.1.1.jar----------commons的日志管理

本文使用了slf4j日志管理,所以需要加入以下包

slf4j-api-1.5.10.jar--------------------------------------------日志管理的增强

slf4j-log4j12-1.5.10.jar---------------------------------------日志包的连接层

所有包如图1springmvc需要的包:

图1:springmvc需要的包

实现的例子还是以helloworld的简单输出,以后再逐步加入数据库的操作,新建一个resource的source folder,用来存放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

 
This is Spring MVC DispatcherServlet
 
SpringMVC DispatchServlet
org.springframework.web.servlet.DispatcherServlet
 
 
SpringContext
contextConfigLocation
 
classpath*:springmvc.xml
 
 
1
 
 
 
SpringMVC DispatchServlet
 
/
 

注意:配置spring的DispatcherServlet的时候,如果需要使用自定义名字的spring配置文件的话,需要在<serlvet>中加入<init-param>这个参数,否则的话spring会默认查找web-inf/classes下面以[servlet-name]-servlet.xml的文件,会出现以下错误:

ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/SpringMVC DispatchServlet-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringMVC DispatchServlet-servlet.xml]

HelloWordController.java:

package org.lxh.mvc.controller;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
@RequestMapping("/test")
public class HelloWordController {
private Logger logger = LoggerFactory.getLogger(HelloWordController.class);
 
@RequestMapping("/hello")
public void Hello() {
logger.info("The hello() method is use");
}
 
@RequestMapping("/welcome")
public String welcome() {
logger.info("The welcome() method is use ");
// return "/welcome";
return "/WEB-INF/jsp/welcome";
}
 
@RequestMapping("/helloWorld")
public ModelAndView helloWorld() {
logger.info("The helloWorld() method is use");
ModelAndView view = new ModelAndView();
// view.setViewName("/helloworld");
// view.setViewName("/WEB-INF/JSP/helloworld");
view.setViewName("/hello");
return view;
}
}

可以发现上面为什么有那么多注释,

先一个一个来说明,spring mvc可以通过以下三种方式链接到视图解析器中,每种方法以requestMapping注解作为跳转.

第一种,使用无返回方法跳转,如果使用返回方法进行跳转的话,则会通过视图解析器进行以prefix(前缀)+方法名+suffix(后缀)组成的页面文件名称.这样的话可能需要用url writer框架进行重向地址更改,保证安全性.

第二种,使用一个返回的字符串方法作为跳转,使用字符串跳转的话好处就是在return的时候可以自己指定返回的名字,JSP组成是prefix(前缀)+返回的字符串+suffix(后缀),这样的话跳转的时候外围广很多而且,安全性相对高点

第三种,返回一个ModelAndView类型,ModelAndView是spring2.5经常使用的方式,使用setViewName方法则可以跳转到指定的页面.

在类中还还发现一个Contorller的注解,该注解是用于把当前Java类变成一个Spring里面的一个beans,如果在Class 上面加入requestMapping的话,而在方法上还拥有requestMapping,则在浏览器输入的地址为/,而在Class上标记了requestMapping的话,则需要在指定的位置创建一个文件夹作为存放.

例如:

输入的话则会查找根目录下面的test文件夹下面的hello.jsp.组成是 prefix(前缀)+/test/+方法名+suffix(后缀).目录结构如图2:

图2:跳转目录

转载于:https://www.cnblogs.com/zhangliang0115/archive/2012/02/20/2359352.html

你可能感兴趣的文章