01_SpringMVC
SpringMVC是什麼
Spring web mvc和Struts2都屬於表現層的框架,它是Spring框架的一部分,我們可以從Spring的整體結構中看得出來:
SpringMVC處理流程
入門程式
新建web專案,命名為
springmvc01
匯入jar包
參考:springMVC-day01\參考資料\jar包\springmvc獨立執行
,所有jar包如下:spring-jdbc-4.1.3.RELEASE.jar spring-jms-4.1.3.RELEASE.jar spring-messaging-4.1.3.RELEASE.jar spring-tx-4.1.3.RELEASE.jar spring-web-4.1.3.RELEASE.jar spring-webmvc-4.1.3.RELEASE.jar commons-logging-1.1.1.jar jstl-1.2.jar spring-aop-4.1.3.RELEASE.jar spring-aspects-4.1.3.RELEASE.jar spring-beans-4.1.3.RELEASE.jar spring-context-4.1.3.RELEASE.jar spring-context-support-4.1.3.RELEASE.jar spring-core-4.1.3.RELEASE.jar spring-expression-4.1.3.RELEASE.jar
配置前端控制器,在
web.xml
中新增:
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 如果沒有指定springMvc核心配置檔案那麼預設會去找/WEB-INF/+<servlet-name>中的內容 + -servlet.xml配置檔案 -->
<!-- 指定springMvc核心配置檔案位置 -->
<!-- tomcat啟動的時候就載入這個servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
啟動tomcat報錯
Could not open ServletContext resource [/WEB-INF/SpringMVC-servlet.xml]
SpringMVC
就是web.xml
中的<servlet-name>
新增SpringMVC核心配置檔案
在
web.xml
中新增
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMvc.xml</param-value>
</init-param>
- 在
Java Resources
下新建一個Source Folder
命名為config
- 在
config
新建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:dubbo="http://code.alibabatech.com/schema/dubbo"
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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置@Controller註解掃描 -->
<context:component-scan base-package="cn.huachao.controller"/>
</beans>
- Pojo
package cn.huachao.domain;
import java.util.Date;
public class Item {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
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 == null ? null : name.trim();
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
}
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 == null ? null : detail.trim();
}
}
- Controller
package cn.huachao.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import cn.huachao.domain.Item;
@Controller
public class ItemController {
@RequestMapping("/list")
public ModelAndView itemsList() throws Exception{
List<Item> itemList = new ArrayList<>();
//商品列表
Item item_1 = new Item();
item_1.setName("聯想筆記本_3");
item_1.setPrice(6000f);
item_1.setDetail("ThinkPad T430 聯想膝上型電腦!");
Item item_2 = new Item();
item_2.setName("蘋果手機");
item_2.setPrice(5000f);
item_2.setDetail("iphone6蘋果手機!");
itemList.add(item_1);
itemList.add(item_2);
//模型和檢視
//model模型: 模型物件中存放了返回給頁面的資料
//view檢視: 檢視物件中指定了返回的頁面的位置
ModelAndView modelAndView = new ModelAndView();
//將返回給頁面的資料放入模型和檢視物件中
modelAndView.addObject("itemList", itemList);
//指定返回的頁面位置
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
}
配置JSP檢視解析器
- 在
SpringMvc.xml
中新增
<!-- InternalResourceViewResolver:支援JSP檢視解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- JstlView表示JSP模板頁面需要使用JSTL標籤庫,所以classpath中必須包含jstl的相關jar 包。
此屬性可以不設定,預設為JstlView。 -->
<!-- spring-webmvc-4.1.3.RELEASE.jar -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
-
ItemController
中setViewName
改變
//modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
modelAndView.setViewName("itemList");
同時支援json和xml
- 新增
spring-oxm-4.1.3.RELEASE.jar
的jar包 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:dubbo="http://code.alibabatech.com/schema/dubbo"
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://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置@Controller註解掃描 -->
<context:component-scan base-package="cn.huachao.controller" />
<context:annotation-config />
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<!-- <property name="defaultContentType" value="text/html"/> -->
<!-- 是否啟用引數支援 ,預設是true-->
<!-- <property name="favorPathExtension" value="true"/> -->
<!-- 是否啟用引數支援 ,預設是true -->
<property name="favorParameter" value="false"/>
<!-- 是否忽略掉accept header ,預設是false -->
<property name="ignoreAcceptHeader" value="true"/>
<property name="mediaTypes">
<map>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"></bean>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>cn.huachao.domain.User</value>
<!-- <value>cn.huachao.domain.Item</value> -->
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
-
User
例項
package cn.huachao.domain;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
private long userID;
private String userName;
private Date birth;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public long getUserID() {
return userID;
}
public void setUserID(long userID) {
this.userID = userID;
}
}
- Controller
@RequestMapping(value="/user/{userid}" , method={RequestMethod.GET})
public String queryUser(@PathVariable("userid")long userID , ModelMap model){
User u = new User();
u.setUserID(userID);
u.setUserName("zhaoyang");
model.addAttribute("User", u);
//mv.setViewName("itemList");
return "User";
}
/WEB-INF/jsp/User.jsp
<body>
UserName: ${requestScope.User.userID } <br />
Age: ${requestScope.User.userName }
</body>
- 測試
-
http://localhost:8080/springmvc01/user/100.json
-
http://localhost:8080/springmvc01/user/100.xml
-
http://localhost:8080/springmvc01/user/100
SpringMVC架構