2021-04-05

.创建动态web工程
导包
SpringMVC独立运行的jar包
2.web.xml文件中加入前端控制器的配置

springMvc org.springframework.web.servlet.DispatcherServlet 1
<!-- 如果没有指定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

至此项目就能正常运行了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值