Spring《错误集合,总结更新》

1.这几天配置springmvc 使用注解,并且自动扫描注解,当我单个配置,不用自动扫描,出现下面错误,找了很多人跟我看,配置也没问题,但是就是显示不出东西,所说的类也去看了,没有问题

這是我的模拟数据类:

 1 package com.itmis.ssm.controller;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.servlet.ModelAndView;
 9 
10 import com.itmis.ssm.po.Items;
11 
12 /**
13  * 注解开发的Handler
14  * 
15  * @author 唐烈
16  * 
17  */
18 // 使用Controller 注解,标识他是个控制器
19 @Controller
20 public class ItemsController3 {
21     // 商品查询列表
22     // @RequestMapping实现对queryItems方法和url进行映射,一个方法对应一个url
23     // 一般建议将url和方法名一样(便于维护)
24     @RequestMapping("/queryItems.action")
25     public ModelAndView queryItems() {
26         List<Items> itemsList = new ArrayList<Items>();
27         Items items1 = new Items();
28         items1.setName("唐小璇");
29         items1.setCreatetime("19960226");
30         items1.setPrice("黄金万两");
31         items1.setId("5");
32         items1.setDetail("都比人士");
33 
34         Items items2 = new Items();
35         items2.setName("麻子哥");
36         items2.setCreatetime("19960226");
37         items2.setPrice("黄金万两");
38         items2.setId("1");
39         items2.setDetail("帅气人士");
40 
41         itemsList.add(items1);
42         itemsList.add(items2);
43 
44         // 新建一个ModelAndView
45         ModelAndView modelAndView = new ModelAndView();
46         // 设置视图取得的数据
47         modelAndView.addObject("itemsList", itemsList);
48 
49         // 设置转发视图的路径
50         modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
51 
52         return modelAndView;
53     }
54     /**
55      * 使用注解模式 這里就可以创建其它方法 比如,商品添加,商品订单,商品删除等等
56      */
57 }

 

我的web.xml配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>SpringMVC001</display-name>
 4   <!-- Spring  服务层的配置文件   <context-param>
 5       <param-name>contextConfigLocation</param-name>
 6       <param-value>classpath:springmvc.xml</param-value>
 7   </context-param>
 8 
 9 -->
10   <!-- 配置前端控制器 (中央处理器)start-->
11   <servlet>
12           <servlet-name>springmvc</servlet-name>
13           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
14           <!-- contextConfigLocation配置springmvc加载的配置文件(映射器,解析器,适配器等待那个)如果不配置contextConfigLocation他默认加载的是/WEB-IFN/servlet名称-servlet.xml(springmvc-servlet.xml) -->
15           <init-param>
16               <param-name>contextConfigLocation</param-name>
17               <param-value>classpath:springmvc.xml</param-value>
18           </init-param>
19   </servlet>
20   <!-- Spring 监听器
21     <listener>
22       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
23   </listener>-->
24   <servlet-mapping>
25     <!-- 
26       第一种:*.action,访问.action结尾DispatcherServlet进行解析
27       第二种:/,所有访问的地址都有DispatcherServlet进行解析,对于静态的文件的解析需要不让DispatcherServlet进行解析,使用此方法可以实现RESTful风格的url
28       第三种:/*,這中配置不对,使用這种配置,最终要发送到jsp页面时,仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到Handler,会报错
29    -->
30       <servlet-name>springmvc</servlet-name>
31      <url-pattern>*.action</url-pattern>
32   </servlet-mapping>
33     <!-- 配置前端控制器 (中央处理器)end-->
34   <welcome-file-list>
35     <welcome-file>index.html</welcome-file>
36     <welcome-file>index.htm</welcome-file>
37     <welcome-file>index.jsp</welcome-file>
38     <welcome-file>default.html</welcome-file>
39     <welcome-file>default.htm</welcome-file>
40     <welcome-file>default.jsp</welcome-file>
41   </welcome-file-list>
42 </web-app>

springmvc.xml配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans 
 3         xmlns="http://www.springframework.org/schema/beans"
 4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5        xmlns:p="http://www.springframework.org/schema/p"
 6        xmlns:context="http://www.springframework.org/schema/context"
 7        xmlns:mvc="http://www.springframework.org/schema/mvc"
 8         xmlns:tx="http://www.springframework.org/schema/tx"
 9         xmlns:aop="http://www.springframework.org/schema/aop"
10           xsi:schemaLocation="http://www.springframework.org/schema/beans 
11                                       http://www.springframework.org/schema/beans/spring-beans.xsd
12                                       http://www.springframework.org/schema/context
13                                     http://www.springframework.org/schema/context/spring-context-3.2.xsd
14                                     http://www.springframework.org/schema/mvc
15                                     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd>
16                                     http://www.springframework.org/schema/tx
17                                     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
18                                     http://www.springframework.org/schema/aop
19                                     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
20     <!-- 配置Handler -->
21          <bean id="itemController1" name="/queryItems_1.action" class="com.itmis.ssm.controller.ItemsController1"></bean>
22          
23     <!-- 配置另外一个Handler  -->  
24     <bean id="itemController2" class="com.itmis.ssm.controller.ItemsController2"/>
25     
26     <!-- 对注解的Handler可以进行单个配置
27         实际开发中建议使用组件扫描,不建议单个配置
28      -->
29      <!--  <bean class="com.itmis.ssm.controller.ItemsController3"/>  -->
30      <!-- 使用组件扫瞄
31          可以扫描controller,service,等等组件
32         這里扫描时controller,指定从他controller的包 use-default-filters默认为true 表示扫描所有注解的@Component的类,并注册成bean.也就是@Component的子注解@Service,@Reposity等
33         <context:include-filter type="annotation" expression=""/>  表示扫描设置的某些注解
34         <context:exclude-filter type="annotation" expression=""/> 表示不扫描设置的某些注解
35     -->
36     <context:component-scan base-package="com.itmis.ssm.controller">
37             
38     </context:component-scan>
39      <!-- 启动Springmvc 的注解 完成对请求对pojo的映射       <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>-->
40     <!--处理器映射器
41         将bean的name作为url进行查找,需要配置Handler时指定你的beanname(指的url)
42      -->     
43         <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />    
44              <!-- 简单url的映射 -->
45      <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
46          <property name="mappings"> <!-- 不知道为什么不能为mapping而必须是mappings或者其他,可能是关键字等。 -->
47              <props>
48                      <prop key="/queryItems1.action">itemController1</prop>
49                      <prop key="/queryItems2.action">itemController1</prop>
50                      <prop key="/queryItems3.action">itemController2</prop>
51              </props>        
52          </property> 
53      </bean>
54     <!--处理器适配器 
55             所有的处理适配器都实现了HandlerAdapter接口  接口:org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
56     -->
57         <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>  
58     <!-- 另一个非注解的适配器 -->
59         <bean class = "org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
60         
61     <!-- 注解的映射器 
62          配置带注解的映射器
63      -->
64          <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
65     <!-- 注解的适配器 
66             配置带注解的适配器
67         -->
68             <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
69     <!-- 使用mvc:annotation-driven代替上面的注解映射器和适配器配置,而且mvc:annotation-driven默认加载了很多参数绑定方法,
70         比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上面的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
71         而我们实际开发中我们使用注解驱动mvc:annotation-driven
72         <mvc:annotation-driven/></mvc:annotation-driven>
73      -->
74     <!--视图解析器 
75         解析jsp视图,默认使用jstl标签,classpath下的有jstl的包
76     -->
77             <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
78     </beans>

最终达到的效果:

解决办法:卡了两天也是在机缘巧合之下才得出的小结论(是要在你配置确定没错的前提下這个可能对你有少许帮助),之前怎么弄都要报错,后面我同步svn时候出错,然后东西包,這些都红叉,想着不想去改,直接把项目在本地干掉,同步了svn 的,把tomcat里面的关于工程的也全部干掉,全部干掉后我把svn 项目同步下来,然后重启项目,玛德跑起来了,没事了,既高兴,有愤怒,也希望能帮到遇到类似情况的朋友,程序有时候就是那么坑爹,如果还是不行呢? 别急,还是不行的话那就是你的jbk问题(jbk1.8会导致這个情况的发生),版本太高导致的,右击你的项目选择properties 把jdk跳低一点。工程可能会报错,不过没事你测试下是不是這样导致的,jdk版本1.8不兼容。

 

 

 2.如果你在运行這个的时候,查阅很多资料,应该还是是你的jdk不兼容所导致的问题,很多时候jdk用高了也不一定是好,最好是用相对于中度的,发布稳定点的,所以解决办法就是:去官网下载jgk1.8一下的安装在电脑上,這里说下,一个电脑可以安装两个jdk的路径可以是你上次那个,实际用的时候自己选择需要的即可,可不可可以安装更多這个你就自己去尝试了,(注意:tomcat jdl版本,和工程运行版本 一致问题)。

 

 

 5.springmvc启动出现 “property” 元素开头类容无效

    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 

原因:没有引入beans,引入上面路径即可

 

 

 

4.问题:Cannot find class 字面意思很好理解,说的是你的类没有找到,這是因为我们在tomcat里面跑的时候在我们classes没有生成相应的.class文件,解决办法:将tomcat里面wtpwebapps里面的工程直接删掉,然后清楚下缓存,重启tomcat,如果重启tomcat不行重启你的Eclipse即可。

 

posted @ 2017-03-22 21:38  人生在与不断学习。  阅读(275)  评论(0编辑  收藏  举报