<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                使用 ContentNegotiatingViewResolver 多視圖解析器可以同時解析多種類型的視圖。 <br/> 下面演示同時能夠解析`.xml`、`.json`、`.jsp`視圖的配置,步驟如下: **1.引入能夠解析xml和json的依賴** ```xml <!-- xml視圖依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>5.2.9.RELEASE</version> </dependency> <!-- json視圖依賴 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-jaxb-annotations</artifactId> <version>2.9.5</version> </dependency> ``` **2. 配置ContentNegotiatingViewResolver** *`resources/springmvc-servlet.xml`* ```xml <!-- 1. 配置ContentNegotiationManagerFactoryBean --> <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <!-- 開啟后綴識別。如識別請求地址 http://localhost:8080/xxx/order.json 的后綴名 .json --> <property name="favorPathExtension" value="true" /> <!-- 開啟請求參數識別。如識別請求地址 http://localhost:8080/xxx?format=json 中format指定的json--> <!-- format是默認參數名 --> <property name="favorParameter" value="true" /> <!-- format是默認參數名,如果想更改為format2可以如下配置,則請求地址應為 http://localhost:8080/xxx?format2=json --> <!-- <property name="parameterName" value="format2"/> --> <!-- 關閉accept頭識別,默認false開啟 --> <property name="ignoreAcceptHeader" value="true" /> <!-- 服務器默認的MediaType類型,不指定默認返回html頁面 --> <property name="defaultContentType" value="text/html" /> <!-- 根據后綴名映射數據類型,如http://localhost:8080/xxx/order.json 就對應application/json--> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </map> </property> </bean> <!-- 2. 配置ContentNegotiatingViewResolver解析器 --> <bean id="contentNegotiatingViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <!-- 如果同時配置了多種類型視圖,則根據order來覺得哪個視圖先被調用,order的值越小越先被調用 --> <property name="order" value="0" /> <property name="contentNegotiationManager" ref="contentNegotiationManager" /> <property name="defaultViews"> <list> <!-- json 視圖 --> <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> <!-- xml 視圖 --> <bean id="xmlView" class="org.springframework.web.servlet.view.xml.MarshallingView"> <constructor-arg> <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="marshallerProperties"> <map> <entry key="jaxb.encoding" value="UTF-8"></entry> </map> </property> <property name="packagesToScan" value="com.learn.springmvc02.pojo" /> <!--<property name="classesToBeBound">--> <!--<list>--> <!--<value>com.learn.springmvc02.pojo.Student</value>--> <!--</list>--> <!--</property>--> </bean> </constructor-arg> </bean> </list> </property> </bean> <!-- 3. 配置JSP視圖解析器,當沒有找到合適的視圖解析器時,因為在這里配置的order是最大的,最后則有jsp視圖解析器進行解析 --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="order" value="1" /> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <!-- prefix和suffix共同決定jsp文件的位置 --> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> <property name="exposeContextBeansAsAttributes" value="true" /> </bean> ``` **3. 實體類** 如果一個實體類需要xml視圖顯示,需要添加注解`@XmlRootElement`、`@XmlElement`。 ```java /** * 提醒:使用注解@XmlElement時,不要使用lombok的@Data注解,否則會報異常 */ @Getter @XmlRootElement(name = "teacher") public class Teacher { private String name; private String gender; private Integer total; private Date born; @XmlElement public void setName(String name) { this.name = name; } @XmlElement public void setGender(String gender) { this.gender = gender; } @XmlElement public void setTotal(Integer total) { this.total = total; } @XmlElement public void setBorn(Date born) { this.born = born; } } ``` **4. controller層** ```java @Controller public class TeacherController { @RequestMapping("/to/type/{suffix}") public String toTeacher(@PathVariable("suffix")String suffix, Model model) { Teacher teacher = new Teacher(); teacher.setName("張三"); teacher.setGender("男"); teacher.setTotal(179); teacher.setBorn(new Date()); model.addAttribute("teacher", teacher); return "teacher"; } } ``` **5. 視圖層** *`webapp/WEB-INF/views/teacher.jsp`* ```html <h1>${teacher.name},${teacher.gender},${teacher.total},${teacher.born}</h1> ``` **6. 測試** (1)http://localhost:8080/mvc/to/type/teach.json 。請求地址后綴為`.json`,界面將看到如下的 json 字符串效果。 ```json {"teacher":{"name":"張三","gender":"男","total":179,"born":1654183013119}} ``` (2)http://localhost:8080/mvc/to/type/teach.xml 。請求地址后綴為`.xml`,界面將看到如下的 xml 效果。 ```xml <teacher> <born>2022-06-02T23:17:49.154+08:00</born> <gender>男</gender> <name>張三</name> <total>179</total> </teacher> ``` (3)http://localhost:8080/mvc/to/type/teach 、http://localhost:8080/mvc/to/type/teach.xlsx 。請求地址不添加后綴,或者添加了不能被解析的后綴,界面將會看到如下的 jsp 頁面效果。 ```html 張三,男,179,Thu Jun 02 23:19:56 CST 2022 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看