问题一:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Failed to introspect bean class [com.blog.controller.UserController] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: javax/mail/MessagingException

出现这种问题的原因是因为,以我个人博客为例,我在注册中用到了这个邮箱工具类,邮箱工具类默认抛出Exception和MessagingException异常,然后我去除了该工具类,但未将异常去掉,然后在项目启动中直接报错。如果是用jsp作为表现层直接到首页或者其他界面直接会报500异常。所以说,我们在Controller中的方法,有时throws异常要慎用。

普遍的解决办法:就是MVC模式以jsp作为表现层,控制器-模型-视图,通过这种视图跳转的方式,邮箱注册发送邮件是没有任何问题的。如果用纯HTML或纯jsp,而不是通过视图跳转到jsp。

正常情况下,视图应该是这样的。

@RequestMapping("test")
public String test(){   return "test"; }

  

通过上述这种方式跳转jsp,而不是直接浏览器输入,例如这样的,localhost:8080/test/test.jsp。当然这样返回也可以,但是不太符合MVC这种方式。

问题二:

描述:为什么返回重复的url呢?明明写了正确的路径。出现这种情况比较多的是由SpringMVC返回JSON数据与前台进行异步交互。‘

问题原因:出现这个的问题的原因是因为我的url正常应该给前台返回json数据,通常在springmvc中url要给前台返回json,必须要定义一个ResponseBody,而我没有定义这个,所以出现了这个异常。

解决办法:

加上ResponseBody时,就能正常访问到JSON数据

如图所示:

不过通常针对控制层而言,如果某个类基本返回的是JSON,建议使用RestController

这样一来不必每一个方法上都添加@ResponseBody

重复性添加该注解是一件很麻烦的事情,总而言之能偷懒就偷点懒。

下面贴一下RestController的源代码:

/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.web.bind.annotation; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import org.springframework.stereotype.Controller; /**
* A convenience annotation that is itself annotated with
* {@link Controller @Controller} and {@link ResponseBody @ResponseBody}.
* <p>
* Types that carry this annotation are treated as controllers where
* {@link RequestMapping @RequestMapping} methods assume
* {@link ResponseBody @ResponseBody} semantics by default.
*
* <p><b>NOTE:</b> {@code @RestController} is processed if an appropriate
* {@code HandlerMapping}-{@code HandlerAdapter} pair is configured such as the
* {@code RequestMappingHandlerMapping}-{@code RequestMappingHandlerAdapter}
* pair which are the default in the MVC Java config and the MVC namespace.
* In particular {@code @RestController} is not supported with the
* {@code DefaultAnnotationHandlerMapping}-{@code AnnotationMethodHandlerAdapter}
* pair both of which are also deprecated.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController { /**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
* @since 4.0.1
*/
String value() default ""; }

通常情况只要在类上面加上诸如RequstMapping或者其他注解,一般都是全局的。

就如RestController源码一样,看它的注解就可以明白它同时具有Controller和ResponseBody两个特性。

关于常用注解这里贴个图,便于回顾:

@Documented的作用:将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档

这里在顺便贴下Controller的源码:

/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.stereotype; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* Indicates that an annotated class is a "Controller" (e.g. a web controller).
*
* <p>This annotation serves as a specialization of {@link Component @Component},
* allowing for implementation classes to be autodetected through classpath scanning.
* It is typically used in combination with annotated handler methods based on the
* {@link org.springframework.web.bind.annotation.RequestMapping} annotation.
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 2.5
* @see Component
* @see org.springframework.web.bind.annotation.RequestMapping
* @see org.springframework.context.annotation.ClassPathBeanDefinitionScanner
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller { /**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
String value() default ""; }

关于这段源码,简单的说其实关键就是上面的注解。

为什么SpringMVC能够通过@Controller+@RequestMapping得到正确的URL

首先不说@RequestMapping,针对如上源码,之所以可以扫描到这个Conroller,关键就在@Component这个注解上。

这个注解可以理解为Struts2的action,对于struts2而言,每个action是一个实例,因此struts2是多例的。多例意味着,每一个对应的action,必须要在配置文件中配置,因此我们可以看到随着项目的扩大,struts2对应的action相关的xml文件会极速增加的。

而SpringMVC是单例,全局共享一个实例,因此通过@Component直接实例为Spring容器。

简单的说,在我们初学spring时,要想将对象交给Spring进行管理,通常要配置如下的bean:

如下bean的作用主要是扫描com.eluzhu.rms.mapper的文件下的数据访问层的代码,通过该代码就不用一个一个写很多bean

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.eluzhu.rms.mapper"/>
</bean>

而@Component在此隐性创建bean,不需要你管理对象,而直接交给spring管理。

这样对于效率的提高是非常有帮助的。

我之所以喜欢SpringMVC的最大原因,是因为简单明了轻量化,struts2的话,当初学习的时候,感觉有不少难度,当然了,学习完后,用的多,习惯就好,但是每次比较烦躁的就是要增加一个类似于Controller作用的类,我都必须要配置一个struts2相关的xml文件。时间久了,CV大法即可搞定,当后来接触SpringMVC时,就发现我再也不想用struts2了。SpringMVC可以说是从Struts2演变而来并借助Spring的名气。

当然了,在校学习Java的同学们,建议还是把Struts2学好,毕竟Struts2的更面向对象。而且涉及不少设计模式。学好Struts2,对理解SpringMVC非常有帮助。

问题三:SpringMVC的配置文件关于注解配置,没有配置好导致本因返回JSON数据,最后却变成了XML。

如图所示:

解决办法:在spring-mvc.xml配置文件,添加如下内容即可解决:

 <!-- FastJson注入 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<!-- FastJson -->
<bean id="fastJsonHttpMessageConverter"
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<!-- 这里顺序不能反,一定先写text/html,不然ie下出现下载提示 -->
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

Springmvc常见问题的更多相关文章

  1. SpringMVC常见问题Error configuring application listener of class org.springframework.web.context.ContextLoaderListenejava.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    六月 20, 2018 9:43:34 下午 org.apache.catalina.core.StandardContext listenerStart 严重: Error configuring ...

  2. SpringMvc常见问题汇总

    1.Service类未用@Service注解2016-10-26 17:31:36 [org.springframework.web.context.ContextLoader]-[ERROR] Co ...

  3. 复习整理9:SpringMVC应用以及源码解析

    一:SpringMVC是什么 SpringMVC只是Spring的一个子框架,作用学过Struts2的应该很好理解,他们都是MVC的框架.学他就是用来代替Struts2的,那么为什么不用Struts2 ...

  4. SpringMVC的应用与工作流程解析

    一:SpringMVC是什么 SpringMVC只是Spring的一个子框架,作用学过Struts2的应该很好理解,他们都是MVC的框架.学他就是用来代替Struts2的,那么为什么不用Struts2 ...

  5. SpringMVC +mybatis+spring 结合easyui用法及常见问题总结

    SpringMVC +mybatis+spring 结合easyui用法及常见问题总结 1.FormatString的用法. 2.用postAjaxFillGrid实现dataGrid 把form表单 ...

  6. SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结

    下载地址: http://pan.baidu.com/s/1qWDinyk 一 开发环境 1.动态web工程 2.部分依赖 hibernate-release-4.1.0.Final.zip hibe ...

  7. springMVC对简单对象、Set、List、Map的数据绑定和常见问题.

    算了,就不粘贴了,到原文去查看吧! springMVC对简单对象.Set.List.Map的数据绑定和常见问题.

  8. SpringMVC 资源国际化实现以及常见问题

    资源国际化可以很方便的实现web项目语言的切换,解决了web项目按需显示不同语言界面的问题. SpringMVC 的资源国际化基于JDK的java.util.ResourceBundle实现,经过Sp ...

  9. spring-mvc 集成 activeMq 常见问题 + 解决方案 (仅供参考)

    最近整合 spring-mvc 和 activeMq ,出现了几个异常,我把他记录下来,具体的原理分析我就不太会写了,只把详细情况和解决方案给出来,希望对各位老铁有所帮助! 问题1:缺少log4j的配 ...

随机推荐

  1. Mysql替换两个字段的内容(字符串)

    问题:用一条sql来替换两个字段的内容 表内容:  待优化sql: update student set name=CONCAT(name,dname),dname=SUBSTR(name FROM ...

  2. 什么是 Spring AOP 和代理

    https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9403056301388627935% ...

  3. BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)

    GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. 如何在Eclipse中修改Git项目

    第一步:先在Eclipse创建一个要上传的项目. 第二步:创建一个Git仓库并与远程仓库相连 第三步:将Eclipse创建的项目复制到创建好的Git仓库 第四步:将Eclipse新建项目删除重新imp ...

  5. java工具包一:日期处理

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7846812.html 邮箱:moyi@moyib ...

  6. JavaScript--事件入门(24)

    // JavaScript事件是由访问Web页面的用户引起的一系列操作; // 例如:用户点击;当用户执行某些操作的时候,再去执行一系列代码; 一 事件介绍 // 事件一般是用于浏览器和用户操作进行交 ...

  7. Tracing 在PeopleSoft 程序中怎么开启

    本文介绍一些常用的跟踪方法在Applications,Application Engine,PeopleSoft,Integration Broker,Cobol中. 1.Application En ...

  8. 结束autocad异常进程

    近日在做CAD自动化数据处理,程序在服务器上运行,运行时间长了会发生异常“autocad application 已停止工作”,这个时候需要通过守护程序去重启CAD, 通过CMD命令“@taskkil ...

  9. 绑定Oracle Database 到 ActiveReport

    ActiveReport 可以和多种数据源交互,包括OLEDB, SQL, XML和集合对象. 在本文中我们将阐述如何绑定Oracle 数据库到 ActiveReport . 这是一件很轻松的事情.下 ...

  10. php 实现简单购物车功能(2)

    上一篇的时候只是写了简单的加入购物车功能,购物车中产品的删除.提交订单后,库存的减少 以及客户账户的余额都没有完善, 这一篇是接着完善上一篇的,上一篇写到了购物车中删除的功能了,为了使删除的代码少敲一 ...