IDE下报IO错:找不到springmvc.xml文件,FileNotFoundException: class path resource [com/springmvcstudy/config/springmvc.xml] cannot be opened because it does not exist
浏览器下报500错误,
HTTP Status 500 - Servlet.init() for servlet springmvc threw exception
IOException parsing XML document from class path resource [com/springmvcstudy/config/springmvc.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/springmvcstudy/config/springmvc.xml] cannot be opened because it does not exist
但是我在web.xml中配置了contextConfigLocation,好像并没有起作用。麻烦前辈们帮忙看下。
springmvc项目结构图
web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet><!--springmvc的前端控制器--> <servlet-name>springmvc</servlet-name> <!--servlet名字--> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- servlet路径--> <init-param> <!-- 指定SpringMVC加载的配置文件(配置处理器映射器,适配器等),若没有配置contextConfigLocation,SpringMVC的配置文件的默认路径是/WEB-INF/springmvc-servlet.xml --> <param-name>contextConfigLocation</param-name> <!----> <param-value>classpath:com/springmvcstudy/config/springmvc.xml</param-value> <!----> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 设置所有以action结尾的请求进入SpringMVC --> <url-pattern>*.action</url-pattern> <!-- 1 /*.action 2 / 所有请求,都由dispatch-servlet解析,但是需要读静态资源设置放行。 3 /* 当转发jsp时,仍然会有dispatchservlet解析,无法根据url找到处理器handle --> </servlet-mapping> </web-app>
springmvc.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--配置controller扫描包--> <context:component-scan base-package="com.springmvcstudy" annotation-config="true"/> <!--配置处理器适配器,所有的处理器适配器都实现HandlerAdapter接口,--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 处理器映射器 --> <!-- 根据bean的name作为url,进行查找Handler 将action的url配置在bean的name中,需要在配置hendler时候指定beanname(就是url) --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!--配置处理器,将配置的handler在spring中进行加载--> <bean name="/queryItems.action" class="com.springmvcstudy.controller.ItemsController"/> <!--视图解析器,解析jsp,默认使用jstl解析,所以classpath:下要用jstl的jar包--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
这是controller
package com.springmvcstudy.controller; import com.springmvcstudy.pojo.items; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.List; /** * Created by huang on 2017/11/20. */ public class ItemsController implements Controller{ public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // conroller---service---impl---dao,调用service查找数据库,此处使用静态数据模拟 List<items> itemses=new ArrayList<items>(); //向list中添加假数据 items items_1 = new items(); items_1.setName("联想笔记本"); items_1.setPrice(6000f); items_1.setDetail("ThinkPad T430 联想笔记本电脑!"); items items_2 = new items(); items_2.setName("苹果手机"); items_2.setPrice(5000f); items_2.setDetail("iphone6苹果手机!"); itemses.add(items_1); itemses.add(items_2); //创建modelAndView准备填充数据、设置视图 ModelAndView modelAndView=new ModelAndView(); //填充数据 modelAndView.addObject("itemses2",itemses); // 设置视图,视图就是渲染数据的媒介,如jsp modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp"); return modelAndView; } }
qq_20151109_0
慕仰2097576
灬java
相关分类