SpringMVC第一次搭建
来源:互联网 发布:毕向东java反射 编辑:程序博客网 时间:2024/05/01 23:27
根据指导书学习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;@Controllerpublic 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
------- 记录完毕 -----
- SpringMVC第一次搭建
- 第一次用springmvc+mybatis搭建环境
- 第一次上手springmvc+mybatis
- SpringMVC 搭建
- SpringMVC搭建
- SpringMVC搭建
- SpringMVC 搭建
- 搭建springmvc
- Springmvc搭建
- SpringMVC搭建
- springmvc搭建
- springMvc搭建
- 搭建SpringMVC
- SpringMVC搭建
- 搭建springMVC
- SpringMVC入门(搭建SpringMVC)
- 第一次搭建WINCE环境
- ssm第一次搭建
- 手机安全卫士04
- DOM和选择器之皮毛关系
- HTML5+CSS3 APP开发之 精彩H5
- Dubbo之旅--集群容错和负载均衡
- [转] 常用牛人主页链接(计算机视觉、模式识别、机器学习相关方向)
- SpringMVC第一次搭建
- python 简单图像处理(13)
- 文章标题
- LeetCode Remove Duplicates from Sorted List & Remove Duplicates from Sorted List II
- 互联网推送服务原理:长连接+心跳机制
- Think In Python Exercise
- 关于VC++6.0中的字符串拼接函数strcat
- J2EE的13规范之我见
- UIPickerView 选取器(滚轮)—IOS开发