(已解决)如何让Spring MVC Controller的同一个方法,根据逻辑判断返回JSON或者HTML视图?

时间:2022-01-07 19:11:40

在web.xml文件中添加

<servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>
            classpath:spring-mvc.xml
            </param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>*.do</url-pattern><url-pattern>*.json</url-pattern><url-pattern>*.xml</url-pattern></servlet-mapping>

即可