三步輕松搭建springMVC框架
一、搭建步驟
1、導入jar包、創(chuàng)建項目包結構
2、在web.xml中配置前端控制器
3、編寫springMvc核心配置文件
4、編寫pojo類和Controller類測試
二、實現(xiàn)
1、導入jar包、創(chuàng)建項目包結構
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)文件上傳功能
相關文章
SpringBoot使用Sa-Token實現(xiàn)登錄認證
本文主要介紹了SpringBoot使用Sa-Token實現(xiàn)登錄認證,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04如何在JDK 9中更簡潔使用 try-with-resources 語句
本文詳細介紹了自 JDK 7 引入的 try-with-resources 語句的原理和用法,以及介紹了 JDK 9 對 try-with-resources 的改進,使得用戶可以更加方便、簡潔的使用 try-with-resources 語句。,需要的朋友可以參考下2019-06-06MyBatis的JdbcType與Oracle、MySql數(shù)據(jù)類型一覽表
這篇文章主要介紹了MyBatis的JdbcType與Oracle、MySql數(shù)據(jù)類型一覽表,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Spring boot基于ScheduledFuture實現(xiàn)定時任務
這篇文章主要介紹了Spring boot基于ScheduledFuture實現(xiàn)定時任務,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06