spring 3.1.0.m 配置MVC

首先导包,用的3.1.0.m包。。之前有个帖子说了这个版本的包的介绍

我就不管三七二十二了,全放进去了


然后就是web.xml配置,主要是配置spring容器

        <!-- 加载spring容器,
        因为后面要加filter,并且注入bean所以用listener加载容器
        因为在web.xml中会涉及到加载顺序导致filter注入bean的时候出异常
        web.xml中加载顺序 context-param -> listener -> filter -> servlet
         -->
        <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/demo.xml</param-value>
	</context-param>                    

        <!-- 拦截所有.do请求交由spring的servlet分发(不知道是不是可以这么说) -->
	<servlet>
		<servlet-name>springContent</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/demo.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springContent</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
然后就是配置spring容器配置文件demo.xml

比较麻烦的是头,这个很关键,我偷懒了,直接全加载了,用的eclipse的spring插件生成,当然牛B的也可以纯手写

<?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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms"
	xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
		http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
		http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

	<!-- 这里的映射有好几种方式,我用顺手的是这个 -->
	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<!-- 把具体请求映射到具体controller的id -->
				<prop key="index.do">demo_index</prop>
			</props>
		</property>
	</bean>
	
	<!-- controller要实现controller的handleRequest方法 -->
	<bean id="demo_index" class="com.netel.web.Demo_index"/>
	
</beans>

然后是Demo_index这个controller

package com.netel.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class Demo_index implements Controller{

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		ModelAndView v = new ModelAndView("index.jsp");
		System.out.println("进入controller");
		return v;
	}

}

结果eclipse报错,说httpservietrequest之类的找不到包,我又偷懒在tomcat的lib下找了个servlet-api.jar导入,完事

发布运行,报错。。干脆利落

java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:616)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
	at org.springframework.web.SpringServletContainerInitializer.<clinit>(SpringServletContainerInitializer.java:104)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at org.apache.catalina.startup.ContextConfig.getServletContainerInitializer(ContextConfig.java:1409)
	at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1328)
	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1226)
	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:313)
	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:89)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4667)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:988)
	at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:771)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:988)
	at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:275)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
	at org.apache.catalina.core.StandardService.startInternal(StandardService.java:427)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
	at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:649)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:139)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:585)
	... 6 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1666)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1511)
	... 29 more

大概是说logfactory找不到,好么,找个commons-logging-1.1.1.jar放入,运行OK.


  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当出现依赖不能自动下载的情况时,有几个可能的原因可以考虑。首先,你可以确认你的Maven配置是否正确。你可以检查你的`settings.xml`文件中是否正确配置了远程仓库,以便Maven能够下载依赖。此外,你还可以尝试运行`mvn clean install`命令来清除本地仓库并重新下载依赖。 另一个可能的原因是插件的冲突。有时候某些插件可能会与你的依赖冲突,导致无法正确下载。你可以尝试卸载或禁用这些插件,然后再次尝试下载依赖。例如,在你的情况下,卸载或禁用MybatisX插件可能会解决问题。 最后,确保你的依赖信息正确无误。检查你的`pom.xml`文件中是否正确声明了依赖项,包括`org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.0`。你可以尝试手动添加依赖,并再次运行Maven来下载它。 综上所述,当遇到依赖不能自动下载的情况时,你可以尝试以下步骤: 1. 检查Maven配置是否正确,包括远程仓库的配置。 2. 运行`mvn clean install`命令来清除本地仓库并重新下载依赖。 3. 卸载或禁用可能与依赖冲突的插件。 4. 确保`pom.xml`文件中依赖信息的正确性,并尝试手动添加依赖。 通过以上步骤,你应该能够解决依赖不能自动下载的问题并成功引入依赖。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Dependency‘org.framework:spring-webmvc:’ not found](https://download.csdn.net/download/weixin_38699492/14036945)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [IDEA-Dependency ‘org.springframework.boot:spring-boot-starter-test:not found 的解决方法](https://blog.csdn.net/shizy102493957/article/details/126242160)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Dependency ‘org.springframework.cloud:spring-cloud-starter-config:‘ not found](https://blog.csdn.net/weixin_44746721/article/details/130411372)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值