Spring MVC —— 簡單使用
阿新 • • 發佈:2019-02-04
在剛使用Spring MVC的時候,大部分的教程顯示的最後結果都是jsp頁面,並沒有反饋json資料,Android開發過程中從伺服器請求的是json/XML資料,如果不知道Spring MVC的元件的結構的話,可以看我上一篇文章Spring MVC —— 整體結構
顯示jsp頁面(此處程式碼參考傳智.關雲長程式碼)
1.新建Web工程
2.修改web.xml
<!-- 配置springmvc的前端控制器 DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name >
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 如果沒有配置,預設是在/WEB-INF/'servletName'-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value >
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 攔截器 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.根據web.xml中的配置,新建springmvc-servlet.xml
4.在Dispatcher Servlet的配置檔案中配置處理器對映器與介面卡
如果不在springmvc-servlet.xml中配置處理器對映器與介面卡,表示使用預設的處理器對映器和介面卡
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- 這個為控制器的包路徑,表示Servlet啟動後掃描該包 -->
<context:component-scan base-package="myserver" />
</beans>
5.編寫處理器Controller
該處使用的註解的方式
@Controller
public class ItemsController2{
@RequestMapping("/queryItems")
public ModelAndView queryItems(){
List<Items> itemList = new ArrayList<Items>();
Items items1 = new Items();
items1.setName("聯想筆記本");
items1.setPrice(6000f);
items1.setDetail("ThinkPad T430 聯想膝上型電腦");
itemList.add(items1);
System.out.println(itemList.get(0).getName());
Items items2 = new Items();
items2.setName("蘋果手機");
items2.setPrice(5000f);
items2.setDetail("iPhone6");
itemList.add(items2);
System.out.println(itemList.get(1).getName());
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList);
modelAndView.setViewName("/WEB-INF/items/itemsList.jsp");
return modelAndView;
}
}
6.配置ViewResolver
在springmvc-servlet.xml中新增
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
7.編寫jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查詢商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryItem.action" method="post">
商品列表:
<table width="100%" border=0>
<tr>
<td> 商品名稱 </td>
<td> 商品價格 </td>
<td> 商品描述 </td>
</tr>
<c:forEach var="item" items="${itemList}">
<tr>
<td> ${item.name} </td>
<td> ${item.price} </td>
<td> ${item.detail} 1111</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
顯示json資料(以前面的為基礎新增)
顯示json資料使用Spring MVC自帶的 @ResponseBody
1、新增jar包(jackson-core-asl.jar、jackson-mapper-asl.jar)
2、在springmvc-servlet.xml中新增
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:component-scan base-package="myserver" />
<!-- 該處為新增語句 -->
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
3、 在java檔案中新增
@RequestMapping("/xmlOrJson")
@ResponseBody
public Map<String, Object> xmlOrJson() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", "hello world");
return map;
}
注:在springmvc-servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
該連結哪些需要添哪些