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
中添加:
SpringMVC
org.springframework.web.servlet.DispatcherServlet
1
SpringMVC
*.action
启动tomcat报错
Could not open ServletContext resource [/WEB-INF/SpringMVC-servlet.xml]
SpringMVC
就是web.xml
中的添加SpringMVC核心配置文件
在
web.xml
中添加
contextConfigLocation
classpath:SpringMvc.xml
- 在
Java Resources
下新建一个Source Folder
命名为config
- 在
config
新建SpringMvc.xml
- 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- 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;
}
}
- 访问地址测试:http://localhost:8080/springmvc01/list.action
配置JSP视图解析器
- 在
SpringMvc.xml
中添加
-
ItemController
中setViewName
改变
//modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
modelAndView.setViewName("itemList");
- 访问地址测试:http://localhost:8080/springmvc01/list.action
同时支持json和xml
- 添加
spring-oxm-4.1.3.RELEASE.jar
的jar包 SpringMvc.xml
cn.huachao.domain.User
-
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
UserName: ${requestScope.User.userID }
Age: ${requestScope.User.userName }
- 测试
- http://localhost:8080/springmvc01/user/100.json
- http://localhost:8080/springmvc01/user/100.xml
- http://localhost:8080/springmvc01/user/100
SpringMVC架构
参考:
SpringMVC原理教程和搭建实例
下一篇
02-SpringMVC