SpringMVC 구성의 사용에 관한 기사가 많이 있지만 명확하게 설명되는 것은 많지 않습니다. 여기서 우리는 주로 일반적으로 사용되는 구성 항목과 구문 분석 클래스의 사용법을 소개합니다. SpringMVC에서 컨텐츠를 처리하는 두 가지 방법이 있습니다. 하나는 컨버터이고, 다른 하나는 뷰레 고가입니다. 둘 다 JSON, XML을 처리하고 컨텐츠 형식을 형성 할 수 있습니다.
<? xml version = "1.0"alcoding = "utf-8"stockalone = "no"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : 컨텍스트 = "http://www.springframework.org/schema/contex" xmlns : aop = "http://www.springframework.org/schema/aop"xmlns : mvc = "http://www.springframework.org/schema/mvc"xmlns : p = "http://www.springframwork.org. xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance"xmlns : util = "http://www.springframework.org/schema/util"xmlns : c = "http://www.spramframwork.org/sschema/c. xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/context http://www.sprampramework.org/schema/context/spring-context-3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.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/schema/oop/spring--sop-3.xsd "! 일반적 으로이 구성은 DispatchServlet에 의해로드되며 스프링 청취 클래스와 동일한 컨텍스트에 있지 않기 때문에 구성을로드해야합니다. 이유를 알고 싶다면 http://blog.csdn.net/strivezxq/article/details/43795081이 기사를 참조하십시오.이 기사는 Spring 초기화 프로세스를 자세히 분석합니다.> <context : context : context : context : "classpath : app.properties"/> <!-Componated Components, @and and and and and and and and and and and scans componated @scans the classpotent @scans the classpotent @ @Controller use-default-filters = "false"는 스캔 한 주석을 설정할 수 있습니다. 일반적으로 SpringMVC 구성은 다음 두 주석을로드합니다.> <context : component-scan base-package = "your.base.package"use-default-filters = "false"> <context : include filter type = "Annotation"expression = "org.springframework.steretype.controller"/> <context : filter = filter = "Annotation" expression = "org.springframework.web.bind.annotation.controlleradvice"/> </context : context : context : component : component-scan 주석-콘피드 = "true"> 컨텍스트의 기능을 이미 포함하고 있으므로 기본적으로 annotations를 구성 할 필요가 없으므로,이 구성이 구성 할 필요가 없다. @required 및 @autowired뿐만 아니라 JSR 250의 @postconstruct, @predestroy 및 @Resource (사용 가능한 경우), Jax-ws의 @webserviceref (사용 가능한 경우), ejb3의 @ejb (사용 가능한 경우) 및 jpa의 @persistencecontext 및 @persistenceunit (a abuse /<! 스프링 MVC 컨텍스트에서 org.springframework.web.servlet.resource.defaultservlethttprequesthandler를 정의합니다. 검사관처럼 DispatcherServlet에 들어가는 URL을 스크리닝합니다. 정적 리소스 인 경우 요청이 웹 애플리케이션 서버의 기본 서블릿으로 전송됩니다. 정적 리소스 요청이 아닌 경우 Dispatcherservlet은 계속 처리됩니다. 웹 애플리케이션 서버의 기본 서플릿 이름은 "기본값"이므로 defaultservlethttprequesthandler를 찾을 수 있습니다. 모든 웹 애플리케이션 서버의 기본 서플렛 이름이 "기본값"이 아닌 경우, 기본 수면 이름 속성을 통해 지정된 것을 표시해야합니다. <servlet-name> default </servlet-name> 3. <Url-pattern>*. <Servlet-name> 기본값 </servlet-name> 11. <Url-pattern>*. css </url-pattern> 12. </servlet-mapping> SpringDefault-Servlet-name이 구성되지 않으면 기본 설정이 지원되고 일반적으로 사용되는 웹 서버는 이미 지원됩니다. org.springframework.web.servlet.resource.resourcehttprequesthandler <bean id = "resourcehttprequesthandler"> <property name = "locations"value = "classpath :/meta-inf/resources/"> </property> </bean> <속성 이름 = "mappings"> <props> <props> key = "/resources/**"> resourcehttprequesthandler </prop> </props> </property> </bean> 다음 태그 구현-> <mvc : resources mapping = "/resources/**"위치 = "/resources/"> </mvc : resources> <!-등록 된 핸드리스트 appings에 적용 할 수 있습니다. 각 inteceptor는 org.springframework.web.servlet.handlerinterceptor 또는 org.springframework.web.context.request.webrequestinterceptor interface-> <mvc : interceptors> <mvc : interceptor> <mvc : 매핑 경로 = "/**"/> <mvc : ""/cssssssss를 구현해야합니다. /> <MVC : 제외 맵핑 경로 = "/js/**"/> <mvc : mvc : 제외 맵핑 경로 = "/images/**"/> <bean/> </mvc : interceptor> </mvc : inripceptors> <!-스프링 mvc @controller에 대한 맵핑 요청에 대한 지원을 켜십시오. org.springframework.web.servlet.config.annotationDivenBeanDefinitionParser Configuration-negotiation-anager는 URL에서 콘텐츠 유형 매개 변수를 설정할 수 있으며 기본 컨텐츠 유형은 <bean id = "contentnegotiationManagerFactoryBean"p : favorPateTenter = "forforpateTenter ="parturameter ""favorPateTenter " p : ingoreaCcepTheader = "true"p : defaultContentType = "application/json"> <속성 이름 = "mediaTypes"> <props> <prop key = "json"> application/json </prop> <prop key = "xml"> application/xml </prop> </props> </properial> </bean>-> <mvc : <mvc : content-negotiation-anager = "contentNegotiationManagerFactoryBean"> <mvc : messe-converters> <ref bean = "stringhttpMessageConverter" /> <ref bean = "jsonhttpmessageconverter" /> <ref bean = "MarshallinghttpMesseConverter" /> < /mvc : 메시지. </mvc : Annotation-Driven> <!-컨텐츠 관리 공장-> <bean p : favorPathextension = "false"p : favorParameter = "true"p : parametername = "format"p : ingoreacceptheader = "true"p : defaultContentType = "application/json"> <props> <props> <props> key="json">application/json</prop> <prop key="xml">application/xml</prop> </props> </property> </bean> <!-- Content parser, you can configure the return parameter type by p:parameterName="format" and configure the default requested content type through p:defaultContentType. C : QualityValue = "0.5"는 컨텐츠 유형의 우선 순위를 설정할 수 있습니다. MVC : 주석 중심 및 주석 메소드 (@RequestBody) 인 경우 다음 구성이 효과적이지 않습니다.> <ean> <속성 이름 = "ContentNegotiationManager"ref = "ContentNegotiationManagerFactoryBean"> < /Property> <속성 이름 = "defaultViews"> <bean> <pothat name = "values ="resultvo " /> />> />>>>>>>>>>> <property"value = "value" 이름 = "ExtractValueFroms"value = "true"/> </bean> <ean> <생성자-arg ref = "jaxb2marshaller"/> <propertype "value ="application/xml "/> </bean> </list> </propert> <!-<속성 이름 ="imporeaccepthther ""indioreaccepthead " jaxb marshaller-> <bean id = "jaxb2marshaller"> <property name = "marshallerproperties"> <map> <enterd key = "jaxb.formatted.output"> <value type = "boolean"> true </value> </enther> <enlication key = "jaxb.encoding"utf-8 "/////</enterd> <allate type ="value " 이름 = "packagestoscan"> <slest> <value> com.api.domain </value> <value> com.api.web.controller.vo </value> </list> </property> </bean> <bean id = "jstlviewresolver"> <property name = "order"value = "2"/> name = "view Class" value = "org.springframework.web.servlet.view.jstlView" /> <속성 이름 = "prefix"value = " /views /" /> <property name = "toldix"value = " /> <속성 이름 ="requestContAttribute "value ="rc " /> < /bean> <!- c : qualityvalue ="0. " 기본값은 1.0입니다. 우선 순위가 높을수록 우선 순위가 높아집니다-> <bean id = "stringhttpMessageConverter"> <property name = "supportedMediAtypes"> <list> <value> text/plain; charset = utf-8 </value> <value> text/html; charset = utf-8 </value> </list> </bean> <bean. id = "JSONHTTPMESSAGECONVERTER" /> <bean id = "MarshallinghttpMessageConverter"> <생성자-arg ref = "jaxb2marshaller" /> <!-<property name = "supportedMediadiate"value = "application /xml"> C : type = "Application"C : subtype = "XML"C : QualityValue = "0.5"/> </util : list> </property> </bean>
SpringMVC는 다음과 같이 JSON 구성 단계를 반환합니다.
1. Jackson.jar 패키지를 추가하십시오
2. ApplicationContext.xml 구성 파일에 다음 코드를 추가합니다.
<!-Passing Returns JSON-> <!-<ean>-> <ean> <속성 이름 = "MessageConverters"> <list> <ref bean = "Mapping JacksonHttpMessAgeConverter"/> </list> </property> </bean> <bean id = "Mapping JacksOnhttpMessageConver"> <Property Name "> <speration name ="> <value> text/html; charset = utf-8 </value> </list> </property> </bean>
3. 컨트롤러에 다음 코드를 추가하십시오
@RequestMapping (value = "/siner/listtree", method = requestmethod.post) @ResponseBody public list getListChinese (모델) {list <user> list = (list <chinesecategory>) commonMgr.Find ( "사용자"); 반환 목록; }리턴 값은 목록 또는 맵 유형 일 수 있습니다
요약
위는 일반적으로 사용되는 구성 및 분석 클래스에 대한이 기사의 모든 내용입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구들은이 사이트를 계속 참조 할 수 있습니다.
구성 파일을 사용하여 속성 값을 주입하고 스프링의 @Value에 대한 코드에 대한 자세한 설명
스프링 구성에서 여러 패키지 스캔 문제 분석
스프링 구성 사용의 수명주기에 대한 자세한 설명
단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!