三步輕松搭建springMVC框架
更新時間:2017年08月10日 16:20:36 作者:~馳~
這篇文章主要教大家三步輕松搭建springMVC框架,具有一定的參考價值,感興趣的小伙伴們可以參考一下
一、搭建步驟
1、導入jar包、創(chuàng)建項目包結(jié)構(gòu)
2、在web.xml中配置前端控制器
3、編寫springMvc核心配置文件
4、編寫pojo類和Controller類測試
二、實現(xiàn)
1、導入jar包、創(chuàng)建項目包結(jié)構(gòu)


2、在web.xml中配置前端控制器
<!-- springMvc前端控制器 --> <servlet> <servlet-name>springMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定springMvc核心配置文件位置 如果沒有指定那么默認就會去"/WEB-INF/+ <servlet-name>標簽中內(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("蘋果手機");
items_2.setPrice(5000f);
items_2.setDetail("iphone6蘋果手機!");
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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 詳解Spring框架之基于Restful風格實現(xiàn)的SpringMVC
- 詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實用
- 手把手教你搭建SpringMVC框架——最小化配置
- 詳解SpringMVC驗證框架Validation特殊用法
- 在SpringMVC框架下實現(xiàn)文件的上傳和下載示例
- Java框架篇:Spring+SpringMVC+hibernate整合開發(fā)
- springMVC框架下JQuery傳遞并解析Json數(shù)據(jù)
- JavaWeb開發(fā)之Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基礎框架
- jquery.form.js框架實現(xiàn)文件上傳功能案例解析(springmvc)
- 使用jQuery.form.js/springmvc框架實現(xiàn)文件上傳功能
相關文章
Java?超詳細講解數(shù)據(jù)結(jié)構(gòu)中的堆的應用
堆首先是一個完全二叉樹,堆分為小根堆和大根堆。小根堆,所有結(jié)點的左右子節(jié)點都不小于根節(jié)點;大根堆,所有結(jié)點的左右子節(jié)點都不大于根節(jié)點。優(yōu)先級隊列(priorityQueue)底層就是一個小根堆2022-04-04
官方詳解HDFS?Balancer工具主要調(diào)優(yōu)參數(shù)
這篇文章主要為大家介紹了HDFS?Balancer工具主要調(diào)優(yōu)參數(shù)的?官方詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03

