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

springmvc 返回json格式数据
<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://springframework.org/schma/util"
	xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

	<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<context:component-scan base-package="com.**.controllers" />
	<!-- spring的默认实现,不配置也一样 -->
	<bean id="handlerMapping"
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="order" value="0" />
	</bean><!--此处重点,绑定json格式化-->
?<bean id="jacksonMessageConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
	</bean>
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="jacksonMessageConverter" />
			</list>
		</property>
	</bean> 
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/jsp/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
		<property name="viewClass">  
            <value>org.springframework.web.servlet.view.JstlView</value>  
        </property>
	</bean>
</beans>
?

controller代码

@Controller
@RequestMapping("/sso/admin/monthParameterController")
public class MonthParameterController {
	@Autowired
	private IOtherParametersService otherParametersService;
	 
	@RequestMapping("/delfn")
	public @ResponseBody JSONObject delFn(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap)   {
		String ids = request.getParameter("ids");
	  
		JSONObject json = new JSONObject();
		try {
			otherParametersService.deleteOtherParameters(ids);
			json.put("result", true);
		} catch (SQLException e) {
		 
			e.printStackTrace();
		}
		
		return json;

	}

}
?

?