SpringMVC構成の使用に関する多くの記事がありますが、多くの記事は明確に説明されていません。ここでは、主に一般的に使用される構成アイテムとその解析クラスの使用法を紹介します。 SpringMVCでコンテンツを処理する方法は2つあります。1つはコンバーターで、もう1つはViewResolverです。どちらもJSON、XML、およびフォームコンテンツ形式を処理できます。
<?xml version = "1.0" encoding = "utf-8" standalone = "no"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:mvc = "http://www.springframework.org/schema/mvc" xmlns:p = " xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns:util = "http://www.springframework.org/schema/util" xmlns:c = " xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/contex http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/AOP http://www.springframework.org/コントローラーで使用されているため、この構成はDispatchServletによってロードされ、Springリスニングクラスと同じコンテキストにないため、構成をロードする必要があります。理由を知りたい場合は、http://blog.csdn.net/strivezxq/article/details/43795081を参照してくださいこの記事では、春の初期化プロセスを詳細に分析します - > <コンテキスト:プロパティ - プレイスホルダー場所= "classpath:app.properties"/>> <! @controller use-default-filters = "false"、どのコメントがスキャンされるかを設定できます。一般的に、SpringMVC構成は次の2つのコメントのみをロードします - > <コンテキスト:Component-Scanベースパッケージ= "Your.base.Package" use-default-filters = "false"> <context:inclug.springframework.tertype.controller "/> <inclute-filter type =" Annotation " expression = "org.springframework.web.bind.annotation.controlleradvice"/> </context:component-scan> <! - <context:component-scan annotation-config = "true">それは既にコンテキストの関数を含んでいます。 @autowired、およびJSR 250の@postconstruct、@predestroy、@Resource(可能であれば)、Jax-WSの@Webserviceref(利用可能な場合)、ejb3の@ejb(可能な場合)、JPAの@persistencecontext、@persistencenit(利用可能な場合) org.springframework.web.servlet.resource.defaultservlethttprequesthandlerスプリングMVCコンテキスト。 InspectorのようにDispatcherServletに入るURLをスクリーニングします。静的リソースであることがわかった場合、リクエストはWebアプリケーションサーバーのデフォルトサーブレットに転送されます。静的なリソース要求でない場合、DispatcherServletは引き続き処理されます。 Webアプリケーションサーバーのデフォルトのサーブレット名は「デフォルト」であるため、defaultservlethttprequesthandlerを見つけることができます。すべてのWebアプリケーションサーバーのデフォルトのサーブレット名が「デフォルト」でない場合、デフォルトのサーブレット名属性を介して指定された属性を表示する必要があります:<MVC:デフォルトセルバーハンドラーデフォルトセルブレットネーム= "Webサーバーで使用されるデフォルトのサーブレット名" <Servlet-Name>デフォルト</servlet-name> 3。<url-pattern>*。jpg</url-pattern> 4。</servlet-mapping> 5。<servlet-name> 6。<servlet-name> default </servlet-name> 7。 <servlet-name> default </servlet-name> 11。<url-pattern>*。css</url-pattern> 12。</servlet-mapping> springdefault-servlet-nameが構成されていない場合、デフォルトの設定がサポートされ、一般的に使用されるWebサーバーが既にサポートされています - > <mvc:<mvc:<mvc hand < org.springframework.web.servlet.resource.resourcehttprequesthandler <bean id = "resourcehttprequesthandler"> <property name = "locations" value = "classpath:/" classpath:/"> </property> </bean> <bean> key = "/resources/**"> resourcehttprequesthandler </prop> </props> </property> </bean>各インテセプターは、org.springframework.web.servlet.handlerinterceptorまたはorg.springframework.web.context.request.webrequestInterceptorインターフェイス - > <MVCを実装する必要があります。 PATH = "/css/**"/> <mvc:除外path = "/js/**"/> <mvc:exclude-mapping path = "/mages/**"/> <bean/> </mvc:intercepor> </mvc:interceptors> <! - マッピングの要求を順調にマッピングするためのサポートをオンにするためのサポートをオンにします@controllers構成解析クラス:org.springframework.web.servlet.config.AnnotationDrivenDefinitionParser Configuration Content-Negotiation-AnagerはURLでコンテンツタイプのパラメーターを設定できます。 P:parametername = "format" p:ingoreacceptheader = "true" p:defaultcontenttype = "application/json"> <プロパティname = "> <props> <prop key =" json "> json </prop> <prop key =" xml "> application <MVC:Annotation-driven Content-Negotiation-Anager = "ContentNegotiationManagerFactoryBean"> <MVC:Message-Converters> <Ref Bean = "StringhttpmessageConverter" /> <Ref Bean = " </mvc:message-converters> </mvc:annotation-driven> <!-- Content Management Factory --> <bean p:favorPathExtension="false" p:favorParameter="true" p:parameterName="format" p:ignoreAcceptHeader="true" p:defaultContentType="application/json"> <property name="mediaTypes"> <props> <prop key = "json"> application/json </prop> <prop key = "xml"> application/xml </prop> </props> </property> </bean> <! - content parser、p:parametername = "format"で戻るパラメータータイプを構成し、P:Defaulttenttypeを介して要求されたコンテンツタイプを設定できます。 C:QualityValue = "0.5"は、コンテンツタイプの優先度を設定できます。 MVC:アノテーションドリブンおよびアノテーションメソッド(@RequestBody)の場合、次の構成は効果的ではありません - > <bean> <プロパティ名= "contentNegotiationManager" ref = "contentNegotiationManagerFactoryBean"> < /プロパティ> <プロパティ名= "デフォルトビューズ"> < name = "extractvaluefromsinglekeymodel" value = "true"/> </bean> <bean> <constructor-arg ref = "jaxb2marshaller"/> <プロパティname = "contentType" value = "application/xml"/>> </bean> </list> </property> < <! - jaxb marshallerを使用したxmlビュー - > <bean id = "jaxb2marshaller"> <プロパティ名= "marshallerproperties"> <map> <entry key = "jaxb.formatted.output"> <value type = "boolean"> "boolean"> true </value> </エントリ> </property> <property name = "packageStoscan"> <list> <value> com.api.domain </value> <balue> com.api.web.controller.vo </value> </list> </property> </bean> <bean id = "jstlviewResolver"> <property name " value = "org.springframework.web.servlet.view.jstlview" /> <property name = "precix" value = " /views /" /> <プロパティ名= "suffix" value = "" /> <プロパティ名= "requestcontextattribute" value = "rc" /> < /bean> <デフォルトは1.0です。優先度が高いほど優先度が高い - > <bean id = "Stringhttpmessageconverter"> <プロパティ名= "supportedmediatypes"> <balue> text/plain; charset = utf-8 </value> text/html; charset = utf-8 </value> id = "jsonhttpmessageconverter" /> <bean id = "marshallinghttpmessageconverter"> <constructor-arg ref = "jaxb2marshaller" /> <! - <プロパティ名= "サポートメディアタイプ" c:type = "application" c:subtype = "xml" c:qualityvalue = "0.5"/> </util:list> </property> </bean>
SpringMVCは、次のようにJSON構成の手順を返します。
1. Jackson.jarパッケージを追加します
2. ApplicationContext.xml構成ファイルに次のコードを追加します
<! - パスリターンズJSON-> <! - <bean> - > <bean> <プロパティ名= "mesageConverters"> <list> <ref bean = "mappingjacksonhttpmessageconverter"/> </list> </property> </bean> <bean id = "mappingsonhttpmessagegecontypes"> <list> <value> text/html; charset = utf-8 </value> </list> </property> </bean>
3.次のコードをコントローラーに追加します
@RequestMapping(value = "/hissh/listtree"、method = requestmethod.post)@responsebody public list getListchinese(モデルモデル){list <user> list =(list <chinesecategory>)commonmgr.find( "from user");返品リスト。 }返品値は、リストまたはマップタイプにすることができます
要約します
上記は、一般的に使用される構成と分析クラスのスプリングのすべての記事のすべての内容です。私はそれが誰にでも役立つことを願っています。興味のある友達は引き続きこのサイトを参照できます:
構成ファイルを使用して属性値を注入するためのコードの詳細な説明と春の@value
スプリング構成で複数のパッケージをスキャンする問題の分析
スプリング構成の使用量のライフサイクルの詳細な説明
欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!