日期:2014-05-16  浏览次数:20389 次

spring3 使用binding 页面json测试

web.xml的配置

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
		/WEB-INF/classes/applicationContext.xml
		</param-value>
	</context-param>
	
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>spring3</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring3</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

?

applicationContext.xml是空的,一般这个配置是处理事务,在这里只是测试spring3 convert json数据到页面,所以就不配置多了。

?

spring3-servlet.xml

?

<context:component-scan base-package="com.lin.controller" />
	<context:annotation-config />
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">     
    <property name="messageConverters">     
        <util:list id="beanList">     
            <ref bean="mappingJacksonHttpMessageConverter" />     
        </util:list>     
    </property>     
	</bean>     
  
	<bean id="mappingJacksonHttpMessageConverter"     
 	class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

?controller.java

@Controller
@RequestMapping("animal")
public class TestJson {

	@RequestMapping("{id}")
	@ResponseBody
	public Map<String,String> receiveJson(@PathVariable int id){
		HashMap<String,String> map = new HashMap<String,String>();
		map.put("dog", "# "+id);
		return map;
	}
	
}

?

完成配置后,在IE地址栏上输入

http://localhost:8080/项目名称/animal/3.txt

就可以页面中看到spring3转换的json数据。