2011-01-15 5 views
4

봄 MVC를 배우려고하는데, 지금까지는 그렇게 좋았지 만 지금은 달라 붙어 있습니다. json을 만들고 javascript (jquery)로 가져 오는 방법을 배우려고합니다.스프링 MVC 컨트롤러 문제

하지만 테스트 목적으로 볼 때 http 요청이 올바르게 표시되어 있는지 확인할 수 있도록 무언가를 만들려고했는데 json을 만들려고 시도 하겠지만 지금까지는 요청을받을 수 없습니다. 작업.

<?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> 

그리고 controllers.xml :

@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 
} 

} 

이 내 서블릿의 context.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 application 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> 

내 전쟁 Test.war라고는, 내가 시도 할 때 localhost:8080/Test 나는 인덱스 뷰를 얻는다. 그러나 상관없이 무엇을의 I 시도 :

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

내가 오류의 일종으로 끝낼, 가장 흥미로운이 하나입니다 많이이 흥미 것으로 판명 내가 봤 한

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

:

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

지금까지 도움이 된 것은 없습니다. 너무 많은 정보를 제공해 죄송합니다.

답변

2

지금까지 내가 당신이 운영자 serlet이 같은이 해결 누락 말할 수 : 당신의 파일 views.xml을 만들 필요가
http://spring-json.sourceforge.net/quick_simpleform.html

이에서

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

테이크 모양을 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

@Mat Banik, 귀하의 회신에 감사드립니다. org.springframework.beans.factory.BeanCreationException : ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]에 정의 된 'jsonViewResolver'라는 이름의 빈을 생성하는 중 에러가 발생했습니다 : 호출 초기화 메소드가 실패했습니다. 중첩 예외는 org.springframework.beans.factory.BeanDefinitionStoreException입니다 : ServletContext 리소스에서 XML 문서를 파싱하는 IOException [/WEB-INF/views.xml]; 중첩 예외는 java.io.FileNotFoundException : ServletContext 리소스 [/WEB-INF/views.xml]를 열 수 없습니다. ' – London

+0

@Mat Banik 업데이트 해 주셔서 감사합니다. 멍청한 것에 대해 사과드립니다. 뷰 대신 view.xml 파일을 명명했습니다. .xml. 나는 또한 평균 시간 안에 arround를 발견했다, 나는 com.google.Gson lib를 찾았다. 그래서 아이템을 반환하는 대신 메소드 리턴 타입을 String으로 변경하고 아이템을이 lib로 문자열로 변환했습니다. 내 페이지에는 json이 있습니다. 어느 것이 더 사용하기 좋은가요? – London

+0

@Mat Banik 감사합니다. – London

관련 문제