問題一: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/MessagingExceptionhtml
出現這種問題的緣由是由於,以我我的博客爲例,我在註冊中用到了這個郵箱工具類,郵箱工具類默認拋出Exception和MessagingException異常,而後我去除了該工具類,但未將異常去掉,而後在項目啓動中直接報錯。若是是用jsp做爲表現層直接到首頁或者其餘界面直接會報500異常。因此說,咱們在Controller中的方法,有時throws異常要慎用。java
廣泛的解決辦法:就是MVC模式以jsp做爲表現層,控制器-模型-視圖,經過這種視圖跳轉的方式,郵箱註冊發送郵件是沒有任何問題的。若是用純HTML或純jsp,而不是經過視圖跳轉到jsp。web
正常狀況下,視圖應該是這樣的。spring
@RequestMapping("test") public String test(){ return "test"; }
經過上述這種方式跳轉jsp,而不是直接瀏覽器輸入,例如這樣的,localhost:8080/test/test.jsp。固然這樣返回也能夠,可是不太符合MVC這種方式。express
問題二:apache
描述:爲何返回重複的url呢?明明寫了正確的路徑。出現這種狀況比較多的是由SpringMVC返回JSON數據與前臺進行異步交互。‘json
問題緣由:出現這個的問題的緣由是由於個人url正常應該給前臺返回json數據,一般在springmvc中url要給前臺返回json,必需要定義一個ResponseBody,而我沒有定義這個,因此出現了這個異常。設計模式
解決辦法:瀏覽器
加上ResponseBody時,就能正常訪問到JSON數據spring-mvc
如圖所示:
不過一般針對控制層而言,若是某個類基本返回的是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>
’