SpringMVC第一次搭建

根据指导书学习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

 

------- 记录完毕 -----

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值