-
创建动态web项目
-
导入jar包
-
配置前端控制器(web.xml中)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>springMvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--springmvc前端控制器-->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--如果没有指定springmvc核心配置文件,那么tomcat启动时默认会去找/WEB-INF/+<servlet-name>中的内容+-servlet.xml配置文件-->
<!--指定SpringMvc核心配置文件的位置-->
<init-param>
<!--全局初始化的本地配置-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringMvc.xml</param-value>
</init-param>
<!--tomcat启动的时候就加载这个Servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
如果没有指定springmvc核心配置文件,那么tomcat启动时默认会去找/WEB-INF/+中的内容±servlet.xml配置文件
- 创建pojo(Items.java)
public class Items {
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(ItemsController.java)
//tomcat启动时扫描该类,需要配置注解Controller,而且在SpringMvc.xml中配置扫描
@Controller
public class ItermsController {
//如果在浏览器中输入http://localhost:8080/springmvc/list.action,通过注解的方式将list.action与itemsList()对应起来。
//struts2是通过配置文件的方式,通过注解的方式更适用于实际的项目开发中。
//此处可以省略.action。但在浏览器中访问时不能省略。在web.xml中的<servlet>中配置了要扫描url,*.action。凡是以.action结尾的请求都进来,然后给Controller。
//指定url到请求方法的映射。
@RequestMapping("/list")
public ModelAndView itemsList() throws Exception{
//模拟数据
List<Items> itemList = new ArrayList<>();
//商品列表
Items items_1 = new Items();
items_1.setName("联想笔记本——3");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430联想笔记本");
Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iPhone6苹果手机");
itemList.add(items_1);
itemList.add(items_2);
//返回到页面
//模型和视图
//model模型:模型兑现中存放了返回给页面的数据
//view视图:视图对象中指定了返回的页面的位置
ModelAndView modelAndView = new ModelAndView();
//将返回给页面的数据放入模型和视图对象中
//第一个参数为页面中要从其中取数据的变量,第二个参数是controller中存放数据的变量
modelAndView.addObject("itemList",itemList);
//指定返回的页面的位置
// modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
//在springmvc.xml中配置了视图解析器(前缀和后缀)
modelAndView.setViewName("itemList");
return modelAndView;
}
}
- 配置注解扫描(springmvc.xml)
<!--配置@Controller注解扫描-->
<!--加载有@Controller注解的类-->
<context:component-scan base-package="cn.zst.cotroller"></context:component-scan>