三步輕松搭建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)就會去"/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模型:模型對象中存放了返回給頁面的數(shù)據(jù)
* view視圖:視圖對象中指定了返回的頁面的位置
*/
//創(chuàng)建ModelAndView對象
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList);
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(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)文章
Maven中pom.xml文件報(bào)錯(cuò)的原因解決
創(chuàng)建Maven項(xiàng)目的時(shí)候,如果你選擇的Packaging為war,那么就會報(bào)錯(cuò),本文主要介紹了Maven中pom.xml文件報(bào)錯(cuò)的原因解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
java8 List<Object>去掉重復(fù)對象的幾種方法
本文主要介紹了java8 List<Object>去掉重復(fù)對象的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Java?超詳細(xì)講解數(shù)據(jù)結(jié)構(gòu)中的堆的應(yīng)用
堆首先是一個(gè)完全二叉樹,堆分為小根堆和大根堆。小根堆,所有結(jié)點(diǎn)的左右子節(jié)點(diǎn)都不小于根節(jié)點(diǎn);大根堆,所有結(jié)點(diǎn)的左右子節(jié)點(diǎn)都不大于根節(jié)點(diǎn)。優(yōu)先級隊(duì)列(priorityQueue)底層就是一個(gè)小根堆2022-04-04
JAVA中Context的詳細(xì)介紹和實(shí)例分析
這篇文章主要介紹了JAVA中Context的詳細(xì)介紹和實(shí)例分析,Context是維持android各組件能夠正常工作的一個(gè)核心功能類。如果感興趣來學(xué)習(xí)一下2020-07-07
Java中實(shí)現(xiàn)訂單超時(shí)自動(dòng)取消功能(最新推薦)
本文介紹了Java中實(shí)現(xiàn)訂單超時(shí)自動(dòng)取消功能的幾種方法,包括定時(shí)任務(wù)、JDK延遲隊(duì)列、Redis過期監(jiān)聽、Redisson分布式延遲隊(duì)列、RocketMQ延遲消息和RabbitMQ死信隊(duì)列,每種方法都有其優(yōu)缺點(diǎn),可以根據(jù)具體需求選擇合適的方法,感興趣的朋友一起看看吧2025-02-02
官方詳解HDFS?Balancer工具主要調(diào)優(yōu)參數(shù)
這篇文章主要為大家介紹了HDFS?Balancer工具主要調(diào)優(yōu)參數(shù)的?官方詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

