基于spring source tools suite注解的spring3.0.x MVC学习笔记(二)

1.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---------------------------------------日志包的连接层

 

2.spring的配置文件跟log4j.xml的日志文件

使用日志管理就必须要加入log4j.xml或者是log4j.properties进行配置,这里采用了Log4j.mxl进行配置

servlet-context.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:beans="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd">

            <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
 
            <!-- 支持spring mvc新的注解类型  -->
            <annotation-driven />

            <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
            <resources mapping="/resources/**" location="/resources/" />

            <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀,在requestmapping输入的地址后自动调用该类进行视图解析 -->
            <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                          <beans:property name="prefix" value="/WEB-INF/views/" />
                          <beans:property name="suffix" value=".jsp" />
             </beans:bean>
             <!-- 对spring 包下的所有注解扫描 -->
             <context:component-scan base-package="com.leefei.test" />
 

</beans:beans>

 

如果需要使用jstl的话则需要在<property>标签中加入

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

prefix:则为前缀,也就是目录的地址, 一般以"WEB-INF/xx"为主,若为 "/" 则为全局使用

suffix:则为后缀,也就是文件名的后缀.使用InternalResourceViewResolver类是只支持jsp,不支持html等其他后缀,如果强制加入其他后缀的话胡出现死循环,至于其他视图的话则以后在使用的时候再介绍

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring/root-context.xml</param-value>
 </context-param>
 
 <!-- Creates the Spring Container shared by all Servlets and Filters -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <!-- Processes application requests -->
 <servlet>
  <servlet-name>appServlet</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/appServlet/servlet-context.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
  
 <servlet-mapping>
  <servlet-name>appServlet</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>

 

注意:配置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]

HomeController.java: 


 

package com.leefei.test;

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,则在浏览器输入的地址为http://访问地址:端口/contextroot/要调用类的requestMapping跳转值/该类中存在的requestMapping/,而在Class上标记了requestMapping的话,则需要在指定的位置创建一个文件夹作为存放.

例如:

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

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值