Spring 2.5和Json(Spring Json视图) [英] Spring 2.5 and Json (Spring Json View)

查看:86
本文介绍了Spring 2.5和Json(Spring Json视图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人尝试过 Spring Json View 教程吗?

我遵循了该教程,但是访问hello.json时出现404错误.

I followed that tutorial, but I've got 404 error while I access hello.json.

我是使用Spring编写Java应用程序的新手,可能我的配置文件存在一些错误?

I'm new to coding a Java application with Spring, may be there is some mistake with my configuration file?

我的项目名称是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>映射到*.htm而不是*.json

这篇关于Spring 2.5和Json(Spring Json视图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆