开发者

Spring mvc controller issue

开发者 https://www.devze.com 2023-02-05 00:40 出处:网络
I\'m trying to learn spring MVC, so far so good but I\'m kind of stuck now. I\'m trying to learn how to create json and get it with javascript(jquery).

I'm trying to learn spring MVC, so far so good but I'm kind of stuck now. I'm trying to learn how to create json and get it with javascript(jquery).

But for testing purposes I tried to create something just so I can see that is displayed properly trough http request, then I'll try to create json and get it, but so far I can't even get the request to work. Here is my controller :

@Controller
@RequestMapping(value="/")
public class IndexController {

@RequestMapping(method=RequestMethod.GET)
    public String index() {
          return "index";
    }

Map<Long,Item> itemMap = createItemMap();

@RequestMapping(value="item/{itemId}", method=RequestMethod.GET)
    public @ResponseBody Item get(@PathVariable Long itemId) {
        Item item = itemMap.get(itemId);
        if (status == null) {
            throw new ResourceNotFoundException(itemId);
        }
        return item;
    }

private Map<Long,Item> createItemMap(){
//omitted because its irrelevant
//I created 2 item objects , with id 1 and 2 for testing purposes
}

}

This is content of my servlet-context.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <!-- Imports user-defined @Controller beans that process client requests -->
    <beans:import resource="controllers.xml" />

</beans:beans>

And controllers.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Scans within the base package of the applic开发者_如何学Pythonation for @Components to configure as beans -->
    <context:component-scan base-package="com.testing.mvc.controller" />

        <!-- Application Message Bundle -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages/messages" />
        <property name="cacheSeconds" value="0" />
    </bean>

</beans>

My war is called Test.war, when I try localhost:8080/Test I get index view which is OK. But regardless of what I try:

localhost:8080/Test/item/1
 localhost:8080/Test/item?itemId=1
 localhost:8080/item?itemId=1

I end up with some kind of error, the most interesting is this one :

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().

I've googled alot found these to be interesting :

http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/ https://src.springframework.org/svn/spring-samples/mvc-ajax/trunk/ Mapping restful ajax requests to spring Spring's Json not being resolved with appropriate response

Nothing helped so far, any idea what I'm missing. Sorry for providing too much info.


As far as I can tell you are missing this resolver like this in your dispatcher serlet:

<bean name="jsonViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
    <property name="order" value="1"/>
 </bean>

Take look at this:
http://spring-json.sourceforge.net/quick_simpleform.html

You have to create file views.xml in yoru WEB-INF direcotry with this content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
      "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>
</beans>
0

精彩评论

暂无评论...
验证码 换一张
取 消