三步轻松搭建springMVC框架

网友投稿 266 2023-04-22


三步轻松搭建springMVC框架

一、搭建步骤

1、导入jar包、创建项目包结构

2、在web.xml中配置前端控制器

3、编写springMvc核心配置文件

4、编写pojo类和Controller类测试

二、实现

1、导入jar包、创建项目包结构

2、在web.xml中配置前端控制器

springMvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:SpringMvc.xml

2

springMvc

*.action

3、编写springMvc核心配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://springframework.org/schema/mvc"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd">

4、编写pojo类和Controller类测试

pojo类代码:

package cn.it.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();

}

}

Controller类代码:

package cn.it.controller;

import java.util.ArrayList;

import java.util.LivzHeFcst;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import cn.it.pojo.Items;

@Controller

public class ItemsController {

//@RequestMapping指定URL到请求方法的映射,例如:

@RequestMapping("/itemsList")

public ModelAndView itemsList(){

ListitemList = 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(ivzHeFctems_1);

itemList.add(items_2);

/*

* 模型和视图:

* model模型:模型对象中存放了返回给页面的数据

* view视图:视图对象中指定了返回的页面的位置

*/

//创建ModelAndView对象

ModelAndView modelAndView = new ModelAndView();

modelAndView.addObject("itemList", itemList);

modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");

return modelAndView;

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:测试dubbo接口的工具(dubbo如何测试)
下一篇:Java Struts图片上传至指定文件夹并显示图片功能
相关文章

 发表评论

暂时没有评论,来抢沙发吧~