三步輕松搭建springMVC框架
一、搭建步驟
1、導(dǎo)入jar包、創(chuàng)建項(xiàng)目包結(jié)構(gòu)
2、在web.xml中配置前端控制器
3、編寫springMvc核心配置文件
4、編寫pojo類和Controller類測試
二、實(shí)現(xiàn)
1、導(dǎo)入jar包、創(chuàng)建項(xiàng)目包結(jié)構(gòu)


2、在web.xml中配置前端控制器
<!-- springMvc前端控制器 --> <servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定springMvc核心配置文件位置 如果沒有指定那么默認(rèn)就會(huì)去"/WEB-INF/+ <servlet-name>標(biāo)簽中內(nèi)容 + -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>
3、編寫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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://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>
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.List;
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(){
List<Items>itemList = new ArrayList<Items>();
//商品列表
Items items_1 = new Items();
items_1.setName("聯(lián)想筆記本_3");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 聯(lián)想筆記本電腦!");
Items items_2 = new Items();
items_2.setName("蘋果手機(jī)");
items_2.setPrice(5000f);
items_2.setDetail("iphone6蘋果手機(jī)!");
itemList.add(items_1);
itemList.add(items_2);
/*
* 模型和視圖:
* model模型:模型對(duì)象中存放了返回給頁面的數(shù)據(jù)
* view視圖:視圖對(duì)象中指定了返回的頁面的位置
*/
//創(chuàng)建ModelAndView對(duì)象
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList);
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Spring框架之基于Restful風(fēng)格實(shí)現(xiàn)的SpringMVC
- 詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實(shí)用
- 手把手教你搭建SpringMVC框架——最小化配置
- 詳解SpringMVC驗(yàn)證框架Validation特殊用法
- 在SpringMVC框架下實(shí)現(xiàn)文件的上傳和下載示例
- Java框架篇:Spring+SpringMVC+hibernate整合開發(fā)
- springMVC框架下JQuery傳遞并解析Json數(shù)據(jù)
- JavaWeb開發(fā)之Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基礎(chǔ)框架
- jquery.form.js框架實(shí)現(xiàn)文件上傳功能案例解析(springmvc)
- 使用jQuery.form.js/springmvc框架實(shí)現(xiàn)文件上傳功能
相關(guān)文章
dubbo如何設(shè)置連接zookeeper權(quán)限
這篇文章主要介紹了dubbo如何設(shè)置連接zookeeper權(quán)限問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot使用Sa-Token實(shí)現(xiàn)登錄認(rèn)證
本文主要介紹了SpringBoot使用Sa-Token實(shí)現(xiàn)登錄認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Spring整合Mybatis具體代碼實(shí)現(xiàn)流程
這篇文章主要介紹了Spring整合Mybatis實(shí)操分享,文章首先通過介紹Mybatis的工作原理展開Spring整合Mybatis的詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-05-05
如何在JDK 9中更簡潔使用 try-with-resources 語句
本文詳細(xì)介紹了自 JDK 7 引入的 try-with-resources 語句的原理和用法,以及介紹了 JDK 9 對(duì) try-with-resources 的改進(jìn),使得用戶可以更加方便、簡潔的使用 try-with-resources 語句。,需要的朋友可以參考下2019-06-06
Java負(fù)載均衡服務(wù)器實(shí)現(xiàn)上傳文件同步
這篇文章主要介紹了Java負(fù)載均衡服務(wù)器實(shí)現(xiàn)上傳文件同步,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
MyBatis的JdbcType與Oracle、MySql數(shù)據(jù)類型一覽表
這篇文章主要介紹了MyBatis的JdbcType與Oracle、MySql數(shù)據(jù)類型一覽表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
詳解Spring Batch 輕量級(jí)批處理框架實(shí)踐
這篇文章主要介紹了詳解Spring Batch 輕量級(jí)批處理框架實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
Spring boot基于ScheduledFuture實(shí)現(xiàn)定時(shí)任務(wù)
這篇文章主要介紹了Spring boot基于ScheduledFuture實(shí)現(xiàn)定時(shí)任務(wù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06

