您的位置:首页 > 编程语言 > Java开发

SpringMVC第一次搭建

2015-06-30 23:48 495 查看
根据指导书学习SpringMVC框架的搭建,目录结构如下:



** ProductController.java代码如下:

package qiuclass.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import qiuclass.domain.Product;

@Controller
public class ProductController {
private static final Log logger=LogFactory.getLog(ProductController.class);

@RequestMapping(value="/product_input")
public String inputProduct(){
logger.info("inputProduct called");
return "ProductForm";
}
}

@Controller注释类型的一个优点在于:一个控制器类可以包含多个请求处理方法

@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上

** Springmvc-config.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<context:component-scan base-package="qiuclass.controller" />
<mvc:annotation-driven />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/*.html" location="/" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

<context:component-scan />指示SpringMVC扫描目标包中的类

<mvc:annotation-driven /> 包括注册用于支持基于注解的控制器的请求处理方法的Bean对象

<mvc:resources /> 指示SpringMVC哪些静态资源需要单独处理,不需要通过dispatcherServlet

注:如果没有<annotation-driven />, <resources />元素会阻止任意控制器被调用

** 配置文件web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

contextConfigLocation:加载指定位置的Spring配置文件,否则就是默认/WEB-INF/springmvc-servlet.xml文件

DispatcherServlet是前置控制器,处理匹配的请求,如: / 所有的请求都映射到DispatcherServlet里处理

利用Tomcat运行服务后,提示错误:org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/config/springmvc-config.xml]; nested exception is java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource

提示springmvc-config.xml配置文件解释异常,看了后面java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource才知道找不到aop这个库,于是在/WEB-INF/lib/中补上spring-aop-4.1.6.RELEASE.jar库文件



重新运行服务通过,输入网址:http://localhost:13808/SpringMVC3/product_input



浏览器能正常显示表单内容。

****************************************************

备注:初学并照着书本做例子,书本的范例中Lib中并不含有spring-aop-4.1.6.RELEASE.jar这个库,并且能正常运行,非常郁闷,花了很多时间去排错,才发现需要添加这个库。这原因可能跟Tomcat,以及SpringMVC的版本有关,书中SpringMVC版本是3.2.1

------- 记录完毕 -----
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: