springmvc入門例項
阿新 • • 發佈:2018-11-10
程式碼執行流程:
瀏覽器中訪問路徑為http://localhost:8080/itemList/itemList.action
.action被web.xml中配置的*.action攔截
*.action這個url-pattern對應servlet-name為springmvc
springmvc對應的servlet-class中配置了springmvc.xml這個配置檔案
在springmvc.xml配置檔案中配置了controller包
包中的controller類加了@Controller註解,所以會掃描這個類
類中的hello方法配置的@RequestMapping為itemList,與位址列訪問的一致
程式碼
jar:
pojo:
public class Item { /** 商品id */ private Integer id; /** 商品名稱 */ private String name; /** 商品建立時間 */ private Date createtime; /** 商品價格 */ private Double price; /** 商品描述 */ private String detail; public Item() { } public Item(Integer id, String name, Date createtime, String detail) { this.id = id; this.name = name; this.createtime = createtime; this.detail = detail; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } }
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/fmt" prefix="fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>查詢商品列表</title> </head> <body> <form action="${pageContext.request.contextPath }/queryItem.action" method="post"> 查詢條件: <table width="100%" border=1> <tr> <td><input type="submit" value="查詢"/></td> </tr> </table> 商品列表: <table width="100%" border=1> <tr> <td>商品名稱</td> <td>商品價格</td> <td>生產日期</td> <td>商品描述</td> <td>操作</td> </tr> <c:forEach items="${itemList }" var="item"> <tr> <td>${item.name }</td> <td>${item.price }</td> <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td> <td>${item.detail }</td> <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </form> </body> </html>
controller:
@Controller
public class ItemControll {
@RequestMapping("itemList")
public ModelAndView hello(){
ModelAndView modelAndView = new ModelAndView();
List<Item> list = Arrays.asList(new Item(1, "海爾", new Date(), "海爾製冷冰箱"),
new Item(1, "格力", new Date(), "格力變頻空調"),
new Item(1, "美的", new Date(), "美的洗衣機"),
new Item(1, "老闆", new Date(), "老闆吸油煙機"));
modelAndView.addObject("itemList", list);
//modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
//springmvc.xml中配置檢視解析器後這裡就不需要字首和字尾
modelAndView.setViewName("itemList");
return modelAndView;
}
}
springmvc.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置controller的掃描包 -->
<context:component-scan base-package="controller"/>
<!-- 配置處理器對映器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 配置處理器介面卡 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 配置註解驅動,相當於同時使用最新處理器對映器和處理器介面卡,對json資料的響應提供支援-->
<mvc:annotation-driven/>
<!-- 配置檢視解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
springmvc架構