มีบทความมากมายเกี่ยวกับการใช้การกำหนดค่า SpringMVC แต่มีการอธิบายไม่มากนัก ที่นี่เราแนะนำการใช้งานรายการการกำหนดค่าที่ใช้กันทั่วไปและคลาสการแยกวิเคราะห์ มีสองวิธีในการประมวลผลเนื้อหาใน SpringMVC หนึ่งคือตัวแปลงและอีกวิธีหนึ่งคือ ViewResolver ทั้งสองสามารถจัดการรูปแบบเนื้อหา JSON, XML และแบบฟอร์ม
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8" standalone = "ไม่"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: บริบท = "http://ww.springframework xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: mvc = "http://www.springframework.org/schema/mvc xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns: util = "http://www.springframework.org/schema/util XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/context http://www.springframework.org/schema/mvc http://www.springframework.org/schema/MVC/SPRING-MVC-3.2.XSD http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema ใช้ในคอนโทรลเลอร์การกำหนดค่าจะต้องมีการโหลดเนื่องจากโดยทั่วไปการกำหนดค่านี้จะถูกโหลดโดย dispatchServlet และไม่ได้อยู่ในบริบทเดียวกับคลาสฟังสปริง หากคุณต้องการทราบเหตุผลโปรดดู http://blog.csdn.net/strivezxq/article/details/43795081 บทความนี้วิเคราะห์กระบวนการเริ่มต้นฤดูใบไม้ผลิในรายละเอียด-> <บริบท: สถานที่ตั้งสถานที่ @Controller Use-Default-Filters = "False" คุณสามารถตั้งค่าความคิดเห็นที่สแกนได้เท่านั้น โดยทั่วไปการกำหนดค่า SpringMVC จะโหลดความคิดเห็นสองข้อต่อไปนี้-> <บริบท: Component-Scan base-package = "Your.base.package" Use-Default-Filters = "False"> <บริบท: Include-filter type = "Annotation" Expression = "org.springframework.steretype.controller expression = "org.springframework.web.bind.annotation.controlleradvice"/> </บริบท: คอมโพเนนต์-สแกน> <!-<บริบท: ส่วนประกอบ-สแกนคำอธิบายประกอบ-config = "จริง" @autowired รวมถึง @PostConstruct ของ JSR 250, @Predestroy และ @Resource (ถ้ามี), @webserviceref ของ jax-ws (ถ้ามี), @EJB ของ EJB3 (ถ้ามี) org.springframework.web.servlet.resource.defaultservlethttprequesthandler ในบริบท MVC ฤดูใบไม้ผลิ มันจะคัดกรอง URL ที่เข้าสู่ DispatchERServlet เหมือนผู้ตรวจสอบ หากพบว่าเป็นทรัพยากรคงที่คำขอจะถูกโอนไปยังเซิร์ฟเล็ตเริ่มต้นของเว็บแอปพลิเคชันเซิร์ฟเวอร์ หากไม่ใช่คำขอทรัพยากรแบบคงที่ DispatchERServlet จะดำเนินการต่อไป ชื่อ servlet เริ่มต้นของเว็บแอปพลิเคชันเซิร์ฟเวอร์คือ "ค่าเริ่มต้น" ดังนั้นค่าเริ่มต้น ServlethttpRequestHandler สามารถค้นหาได้ หากชื่อเซิร์ฟเวอร์เริ่มต้นของเซิร์ฟเวอร์แอปพลิเคชันเว็บทั้งหมดของคุณไม่ใช่ "เริ่มต้น" คุณจะต้องแสดงแอตทริบิวต์ที่ระบุผ่านทางบริการ-เซิร์ฟเวอร์-ชื่อ: <MVC: ค่าเริ่มต้น Servlet-Handler Default-Servlet-name = "ชื่อเซิร์ฟเล็ตเริ่มต้นที่ใช้โดยเว็บเซิร์ฟเวอร์" /> Tomcat, Jetty, JBoss <servlet-name> ค่าเริ่มต้น </servlet-name> 3. <url-pattern>*. jpg </url-pattern> 4. </servlet-mapping> 5. <servlet-mapping> 6. <servlet-name> ค่าเริ่มต้น </servlet-name> 7. <url-pattern>*. js </url-pattern> 8 <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> key = "/resources/**"> ResourcehtttprequestHandler </prop> </props> </property> </ebean> การติดตั้งแท็กต่อไปนี้-> <mvc: การแมปทรัพยากร = "/ทรัพยากร/**" ตำแหน่ง = "/ทรัพยากร/"> </mvc: ทรัพยากร> <! แต่ละคนรับสัญญาณจะต้องใช้ org.springframework.web.servlet.handlerinterceptor หรือ org.springframework.web.context.request.webrequestinterceptor-> <mvc: interceptors> <mvc: interceptor> <mvc path = "/css/**"/> <mvc: exclude-mapping path = "/js/**"/> <mvc: exclude-mapping path = "/images/**"/> <bean/> </mvc: interceptor> ในทุก @Controllers การกำหนดค่าการแยกวิเคราะห์คลาส: org.springframework.web.servlet.config.annotationDrivenDefinitionParser การกำหนดค่าเนื้อหาการเจรจาต่อรองสามารถตั้งค่าพารามิเตอร์ประเภทเนื้อหาใน URL และประเภทเนื้อหาเริ่มต้น p: fanderparameter = "true" p: parameterName = "format" p: borgeaCceptheader = "true" p: defaultContentType = "แอปพลิเคชัน/json"> <property name = "mediaTypes"> <props> <prop cey = "JSON"> แอปพลิเคชัน/json < </ebean>-> <MVC: คำอธิบายประกอบการเจรจาต่อรอง-การเจรจาต่อรอง = "ContentNegoTiationManagerFactoryBean"> <MVC: Message-converters> <ref Bean = "StringhttpmessageConverter" /> <ref bean = "jsonhtpmessageconverter </mvc: ข้อความแปลงข้อความ> </mvc: คำอธิบายประกอบ-ไดรฟ์> <!-โรงงานจัดการเนื้อหา-> <ถั่ว p: feltpathrextension = "false" p: favorparameter = "true" p: parameterName = "format" P: Proptement " key = "json"> แอปพลิเคชัน/json </prop> <prop key = "xml"> แอปพลิเคชัน/xml </prop> </props> </poreport> </ebean> <!- ตัวแยกวิเคราะห์เนื้อหาคุณสามารถกำหนดค่าพารามิเตอร์การส่งคืนโดย p: parameterName = "รูปแบบ" C: QualityValue = "0.5" สามารถตั้งค่าลำดับความสำคัญของประเภทเนื้อหา ถ้า MVC: วิธีการที่ขับเคลื่อนด้วยคำอธิบายประกอบและคำอธิบายประกอบ (@requestbody) การกำหนดค่าต่อไปนี้ไม่มีผลบังคับใช้-> <bean> <ชื่อคุณสมบัติ = "ContentNegoTiationManager" ref = "ContentNegoTiationManageFactoryBean"> < /คุณสมบัติ> name = "ExtractValueFromsingLekeModel" value = "true"/> </ebean> <bean> <constructor-Arg ref = "jaxb2marshaller"/> <property name = "contentType" value = "แอปพลิเคชัน/xml"/> </ebean> <!-มุมมอง XML โดยใช้ jaxb marshaller-> <bean id = "jaxb2marshaller"> <property name = "marshallerProperties"> <ap> <entry key = "jaxb.formatted.utput"> </map> </คุณสมบัติ> <property name = "Packagestoscan"> <slist> <value> com.api.domain </value> <value> com.api.web.controller.vo </value> </list> </property> </bean> value = "org.springframework.web.servlet.view.jstlView" /> <ชื่อคุณสมบัติ = "คำนำหน้า" value = " /views /" /> <ชื่อคุณสมบัติ = "คำต่อท้าย" value = " /> <property name =" requestcontexttribute "value =" rc " /> ค่าเริ่มต้นคือ 1.0 ลำดับความสำคัญที่สูงขึ้นลำดับความสำคัญที่สูงขึ้น-> <bean id = "stringhttpmessageConverter"> <property name = "supportedediatypes"> <list> <ales> text/plain; charset = utf-8 </alue> id = "jsonhttpmessageConverter" /> <bean id = "marshallinghttpmessageConverter"> <constructor-arg ref = "jaxb2marshaller" /> <! c: type = "แอปพลิเคชัน" c: subtype = "xml" c: qualityvalue = "0.5"/> </itil: รายการ> </porement> </ebean>
SpringMVC ส่งคืนขั้นตอนการกำหนดค่า JSON ดังนี้:
1. เพิ่มแพ็คเกจ Jackson.jar
2. เพิ่มรหัสต่อไปนี้ลงในไฟล์การกำหนดค่า ApplicationContext.xml
<!-ส่งคืน JSON-> <!-<Bean>-> <Bean> <property name = "MessageConverters"> <list> <ref bean = "MappingJacksonhttpmessageConverter"/> </list> </property> </bean> <slist> <value> text/html; charset = utf-8 </value> </list> </porement> </ebean>
3. เพิ่มรหัสต่อไปนี้ลงในคอนโทรลเลอร์
@RequestMapping (value = "/Chinese/ListTree", method = requestMethod.post) @ResponseBody รายการสาธารณะ GetListChinese (รุ่นโมเดล) {รายการ <ผู้ใช้> รายการ = (รายการ <chinesecategory>) CommonMgr.Find ("จากผู้ใช้"); รายการคืน; -ค่าส่งคืนสามารถเป็นรายการหรือประเภทแผนที่
สรุป
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้เกี่ยวกับการกำหนดค่าที่ใช้กันทั่วไปและคลาสการวิเคราะห์ของฤดูใบไม้ผลิ ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน เพื่อนที่สนใจสามารถอ้างถึงเว็บไซต์นี้ต่อไปได้:
คำอธิบายโดยละเอียดของรหัสสำหรับการฉีดค่าแอตทริบิวต์โดยใช้ไฟล์การกำหนดค่าและ @Value ในฤดูใบไม้ผลิ
การวิเคราะห์ปัญหาการสแกนหลายแพ็คเกจในการกำหนดค่าฤดูใบไม้ผลิ
คำอธิบายโดยละเอียดเกี่ยวกับวงจรชีวิตของการใช้การกำหนดค่าฤดูใบไม้ผลิ
หากมีข้อบกพร่องใด ๆ โปรดฝากข้อความไว้เพื่อชี้ให้เห็น ขอบคุณเพื่อนที่ให้การสนับสนุนเว็บไซต์นี้!