问题一: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. 数据结构与算法--最小生成树之Kruskal算法

    数据结构与算法--最小生成树之Kruskal算法 上一节介绍了Prim算法,接着来看Kruskal算法. 我们知道Prim算法是从某个顶点开始,从现有树周围的所有邻边中选出权值最小的那条加入到MST中 ...

  2. 过滤器模式(Filter Pattern)

    过滤器模式 一.什么是过滤器模式   过滤器模式(Filter Pattern),这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来.这种类型的设计模式属于结构型 ...

  3. vue + element ui 实现实现动态渲染表格

    前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...

  4. POJ2955(KB22-C 区间DP)

    Brackets Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 7823Accepted: 4151 Description We ...

  5. LOJ#6085. 「美团 CodeM 资格赛」优惠券(set)

    题意 题目链接 Sol 考虑不合法的情况只有两种: 进去了 再次进去 没进去 但是出来了 显然可以用未知记录抵消掉 直接开个set维护一下所有未知记录的位置 最优策略一定是最后一次操作位置的后继 同时 ...

  6. JS--我发现,原来你是这样的JS(引用类型不简单[上篇],且听我娓娓道来)

    一.介绍 没错,这是第五篇,到了引用类型,这次要分成两次博文了,太多内容了,这是前篇,篇幅很长也很多代码,主要讲引用类型和常用的引用类型,代码试验过的,老铁没毛病. 坚持看坚持写,不容易不容易,希望大 ...

  7. Postman Google浏览器离线安装Postman插件

    Google浏览器离线安装Postman插件 by:授客 QQ:1033553122 解决无法通打开谷歌web商店安装Postman插件的问题,文章参考网络. 测试环境:ChromeStandalon ...

  8. 【Java入门提高篇】Day4 Java中的回调

    又忙了一周,事情差不多解决了,终于有可以继续写我的博客了(各位看官久等了). 这次我们来谈一谈Java里的一个很有意思的东西——回调. 什么叫回调,一本正经的来讲,在计算机程序设计中,回调函数是指通过 ...

  9. APP性能测试指标和测试方法

    流量 常用方法 方法一:Android系统自带统计功能(总体流量数值) Proc/uid_stat/{UID}/tcp_snd和tcp_rcv UID是每个app安装时候分配的唯一编号用于识别该app ...

  10. HBase的写事务,MVCC及新的写线程模型

    MVCC是实现高性能数据库的关键技术,主要为了读不影响写.几乎所有数据库系统都用这技术,比如Spanner,看这里.Percolator,看这里.当然还有mysql.本文说HBase的MVCC和0.9 ...