.创建动态web工程
导包
SpringMVC独立运行的jar包
2.web.xml文件中加入前端控制器的配置
<!-- 如果没有指定springMvc核心配置文件那么默认会去找/WEB-INF/+<servlet-name>中的内容 + -servlet.xml配置文件 -->
springMvc *.action 3.创建SpringMvc.xml文件 项目根目录创建config文件夹,里面创建SpringMvc.xml,注意使用source folder创建。 <?xml version="1.0" encoding="UTF-8"?> 4.web.xml文件中指定SpringMvc核心配置文件位置
springMvc org.springframework.web.servlet.DispatcherServlet
<!-- 如果没有指定springMvc核心配置文件那么默认会去找/WEB-INF/+<servlet-name>中的内容 + -servlet.xml配置文件 -->
<!-- 指定springMvc核心配置文件位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:SpringMvc.xml</param-value>
<!-- <param-value>classpath:SpringMvc.xml</param-value> -->
</init-param>
<!-- tomcat启动的时候就加载这个servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
发现是由于classpath不是指向resource路径,导致一直找不到文件。需要在classpath后面加个**,这样就解决问题了。
5.SpringMvc.xml文件注解扫描开启,新建pojo类,和controller类并加上@Controller注解
<context:component-scan base-package="cn.itheima.controller"></context:component-scan>
package cn.itheima.pojo;
import java.util.Date;
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();
}
}
package cn.itheima.controller;
@Controller
public class ItemsController {
}
6.controller类的内容
package cn.itheima.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.itheima.pojo.Items;
@Controller
public class ItemsController {
// 指定url到请求方法的映射
// url中输入一个地址,找到这个方法.例如:localhost:8081/springmvc0523/list.action
@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();
// 将返回给页面的数据放入模型和视图对象中
modelAndView.addObject("itemList", itemList);
// 指定返回的页面位置
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
}7.启动项目,报错404
还少了一步,没有设置classpath的路径,得把config也设置到WEB-INF目录下
创建config文件目录时,创建source folder项,也可以把新建的目录加到classpath中。
把config目录加入到classpath
至此项目就能正常运行了