这篇文章将为大家详细讲解有关教你快速搭建一个springMVC框架,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。一、搭建步骤导入jar包、创建项目包结构在WEB.xml中配置前端控制器编写s
这篇文章将为大家详细讲解有关教你快速搭建一个springMVC框架,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
一、搭建步骤
编写pojo类和Controller类测试
二、实现
导入jar包、创建项目包结构
2、在web.xml中配置前端控制器
<!-- springMvc前端控制器 --><servlet><servlet-name>springMvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 指定springMvc核心配置文件位置如果没有指定那么默认就会去"/WEB-INF/+ <servlet-name>标签中内容 + -servlet.xml"中找例如:"/WEB-INF/springMvc-servlet.xml"--><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:SpringMvc.xml</param-value></init-param><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>springMvc</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping>
编写springMvc核心配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="Http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置@Controller注解扫描 --><context:component-scan base-package="cn.it.controller"></context:component-scan></beans>
编写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.List;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import cn.it.pojo.Items;@Controllerpublic class ItemsController {//@RequestMapping指定URL到请求方法的映射,例如:@RequestMapping("/itemsList")public ModelAndView itemsList(){List<Items>itemList = new ArrayList<Items>();//商品列表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);//创建ModelAndView对象ModelAndView modelAndView = new ModelAndView();modelAndView.addObject("itemList", itemList);modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");return modelAndView;}}
关于教你快速搭建一个springMVC框架就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
--结束END--
本文标题: 教你快速搭建一个springMVC框架
本文链接: https://www.lsjlt.com/news/224280.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0