SpringMVC的controller向jsp頁面傳json資料
阿新 • • 發佈:2018-12-30
需要匯入這三個jar包
需要在spring.xml檔案新增
<!-- 啟動註解 -->
<mvc:annotation-driven/>
我的springmvc-config.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 啟動註解 --> <mvc:annotation-driven/> <!-- 配置自動掃描的包 --> <context:component-scan base-package="com.hp.action"></context:component-scan> <!-- 配置檢視解析器 把Controller方法返回的檢視解析為實際檢視 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 字首 --> <property name="prefix" value="/WEB-INF/jsp/"></property> <!-- 字尾 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
我的json方法
@RequestMapping(value="/json",method=RequestMethod.GET,produces="application/json")
@ResponseBody
public HashMap hello9(){
HashMap s = new HashMap();
UserBean bean = new UserBean();
bean.setName("張三");
bean.setPwd("123");
s.put("aaa", bean);
return s;
}
由於我在專案訪問前添加了路徑
@RequestMapping(value="/text")
所以訪問前加上了/text 效果圖