简体   繁体   中英

Spring MVC + apache 2 tiles return json object

I configured Spring MVC with apache Tiles.

 <context:component-scan base-package="controllers"/>

<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
    <!--<property name="prefix" value="/WEB-INF/pages/"/>-->
    <!--<property name="suffix" value=".jsp"/>-->
<!--</bean>-->

<bean id="tilesConfigure" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tailesconfig/general.xml</value>
        </list>
    </property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</bean>



<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />

Inside the controller I want to return Json object.

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();

@RequestMapping(value = "/greeting",produces = "application/json")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
            String.format(template, name));
}

}

But when i requesting http://localhost:8081/greeting , i get : Could not resolve view with name 'greeting' in servlet with name 'mvc-dispatcher' Sorry for my English and thanks for any help!

看看spring的ContentNegotiatingViewResolver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM