Springmvc常见问题
问题一: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常见问题的更多相关文章
- 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 ...
 - SpringMvc常见问题汇总
		
1.Service类未用@Service注解2016-10-26 17:31:36 [org.springframework.web.context.ContextLoader]-[ERROR] Co ...
 - 复习整理9:SpringMVC应用以及源码解析
		
一:SpringMVC是什么 SpringMVC只是Spring的一个子框架,作用学过Struts2的应该很好理解,他们都是MVC的框架.学他就是用来代替Struts2的,那么为什么不用Struts2 ...
 - SpringMVC的应用与工作流程解析
		
一:SpringMVC是什么 SpringMVC只是Spring的一个子框架,作用学过Struts2的应该很好理解,他们都是MVC的框架.学他就是用来代替Struts2的,那么为什么不用Struts2 ...
 - SpringMVC +mybatis+spring 结合easyui用法及常见问题总结
		
SpringMVC +mybatis+spring 结合easyui用法及常见问题总结 1.FormatString的用法. 2.用postAjaxFillGrid实现dataGrid 把form表单 ...
 - SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结
		
下载地址: http://pan.baidu.com/s/1qWDinyk 一 开发环境 1.动态web工程 2.部分依赖 hibernate-release-4.1.0.Final.zip hibe ...
 - springMVC对简单对象、Set、List、Map的数据绑定和常见问题.
		
算了,就不粘贴了,到原文去查看吧! springMVC对简单对象.Set.List.Map的数据绑定和常见问题.
 - SpringMVC 资源国际化实现以及常见问题
		
资源国际化可以很方便的实现web项目语言的切换,解决了web项目按需显示不同语言界面的问题. SpringMVC 的资源国际化基于JDK的java.util.ResourceBundle实现,经过Sp ...
 - spring-mvc 集成 activeMq  常见问题 + 解决方案 (仅供参考)
		
最近整合 spring-mvc 和 activeMq ,出现了几个异常,我把他记录下来,具体的原理分析我就不太会写了,只把详细情况和解决方案给出来,希望对各位老铁有所帮助! 问题1:缺少log4j的配 ...
 
随机推荐
- python学习之老男孩python全栈第九期_day002作业
			
1. 判断下列逻辑语句的True,False.(1) 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6Tru ...
 - web自动化开发环境配置详解
			
1.安装 nodejs Grunt和所有grunt插件都是基于nodejs来运行的, https://nodejs.org/ 安装完成之后在终端 node -v 查看安装版本 2.安装 grunt-C ...
 - java中获取系统变量
			
System.getProperty("java.version") 用来获取系统变量.getProperty()这个方法是获取指定键指示的系统属性的.以下是通过System.ge ...
 - webpack4.0在Mac下的安装配置及踩到的坑
			
一.什么是webpack是一个前端资源加载/打包工具.它将根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源.它做的事情是,分析你的项目结构,找到JavaScript模块以 ...
 - vscode sync插件 在不同设备 同步的坑
			
sync的好处不言而喻,在不同的设备都可以同步自己的插件和所有配置: 但是有时有总是会有坑, 现在把我遇到的坑记录下来,以防再次踩坑 VSCode 同步方案 VSCode 的插件 Setting Sy ...
 - Android开发工具Android Studio、Android SDK和Genymotion完全配置
			
所谓“工欲善其事,必先利其器”.Android Studio 是谷歌推出一个Android集成开发工具,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提 ...
 - Fusion 360教程合集27部
			
Fusion 360教程合集27部 教程格式:MP4和flv 等格式 使用版本:教程不是一年出的教程,各个版本都有 (教程软件为英文版) 教程格式:MP4.FLV等视频格式 清晰度:可以看清软件上的文 ...
 - Knockout学习,添加模板,事件,Mouseover,mouseout
			
<div class="rtitle">我的收藏</div> <div class="list_ul" data-bind=&qu ...
 - Azure 中的 Windows 虚拟机概述
			
Azure 虚拟机 (VM) 是 Azure 提供的多种可缩放按需分配计算资源之一. 通常情况下,如果需要以更大的力度(相对于其他控制选项)控制计算环境,则应选择 VM. 本文介绍创建 VM 之前的注 ...
 - Microsoft SQL 关系数据库的使用指南
			
Microsoft SQL 关系数据库的使用指南 https://docs.microsoft.com/zh-cn/sql/relational-databases/database-features