gpt4 book ai didi

json - Spring 2.5 和 Json(Spring Json View )

转载 作者:行者123 更新时间:2023-12-02 02:30:49 25 4
gpt4 key购买 nike

有没有人试过Spring Json View教程?

我按照该教程进行操作,但在访问 hello.json 时出现 404 错误。

我刚开始使用 Spring 编写 Java 应用程序,可能是我的配置文件有问题吗?

我的项目名称是jsonapp

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>jsonapp</display-name>
<servlet>
<servlet-name>jsonapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jsonapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>

jsonapp-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="com.tutorial.json.web" />

<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

<bean name="viewResolver"
class="org.springframework.web.servlet.view.XmlViewResolver" />

</beans>

view.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>
</beans>

HelloController.java

package com.tutorial.json.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/hello.json")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView hello() {
Map<String, String> model = new HashMap<String, String>();
model.put("firstname", "Peter");
model.put("secondname", "Schmitt");

return new ModelAndView("jsonView", model);
}
}

谢谢你的帮助

最佳答案

<url-pattern>在你的 web.xml 中映射到 *.htm而不是 *.json

关于json - Spring 2.5 和 Json(Spring Json View ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3852448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com