SpringMVC之入门程序

SpringMVC之入门程序——使用浏览器展示商品数据

springMVC执行流程(图片来源:https://www.jianshu.com/p/8a20c547e245)

 

1.创建pojo(商品实体类)...

 

2.导包

 

3.配置web.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>springmvc-01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  
  <!-- 前端控制器 -->
  <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <!-- 默认找/WEB-INF/[servlet的名称(name)]-servlet.xml -->
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:springmvc.xml</param-value>
          </init-param>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <!--
          1. /* 拦截所有 jsp js png .css.... 真的全部拦截   建议不使用
          2. *.action   *.do 拦截以 do  action 结尾的请求  肯定能使用
          3. / 拦截所有(不包含jsp)(包含.js,.png,.css...) 强烈建议使用  /对静态资源放行
        -->
        <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>
复制代码

4.配置springmvc.xml核心配置文件

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://www.springframework.org/schema/beans" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.2.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
     
     
     <!-- 扫描@Controller、@Service -->
     <context:component-scan base-package="deep"></context:component-scan>
     
     
</beans>
复制代码

5.创建Controller层

复制代码
package deep.springmvc.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import deep.springmvc.pojo.Items;

/**
 * 商品管理
 * @author DeepSleeping
 *
 */
@Controller
public class ItemController {
    
    //入门程序(使用浏览器展示商品列表)
    @RequestMapping(value = "/item/itemlist.action")
    public ModelAndView itemList(){
        
        //创建页面需要显示的商品数据
        List<Items> list = new ArrayList<Items>();
        list.add(new Items(1, "1华为荣耀8", 2399f, new Date(), "质量好! 1"));
        list.add(new Items(2, "2华为荣耀8", 2399f, new Date(), "质量好! 2"));
        list.add(new Items(3, "3华为荣耀8", 2399f, new Date(), "质量好! 3"));
        list.add(new Items(4, "4华为荣耀8", 2399f, new Date(), "质量好! 4"));
        list.add(new Items(5, "5华为荣耀8", 2399f, new Date(), "质量好! 5"));
        list.add(new Items(6, "6华为荣耀8", 2399f, new Date(), "质量好! 6"));
        
        
        ModelAndView mav = new ModelAndView();
        //数据
        mav.addObject("itemList", list);
        
        
        mav.setViewName("/WEB-INF/jsp/itemList.jsp");
        return mav;
    }
}
复制代码

6.导入jsp页面,获取数据

 

posted @   瓶子coder  阅读(177)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示