当前位置: 首页 > news >正文

SpringMVC第一个例子

暂时先从加载静态数据开始

第一步:web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name></display-name><!-- springmvc的前端控制器的配置 --><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 通过ContextConfigLocation配置需要加载的配置文件 配置文件需要配置适配器映射器 等等 如果不指定则默认加载WEB-INF/Servlet 名字-servlet.xml。--><init-param><param-name>ContextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param></servlet><!-- 第一种 *.action 访问以action结尾的由dispatcherServlet来解析第二种:/所有访问的地址都有dispatcherServlet来解析  静态的文件解析需要配置不让dispatcherServlet来解析使用此方式可以实现restful风格的url第三种:/*这样配置不对        --><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.action</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>

load-on-startup:表示servlet随服务启动;

url-pattern:*.action的请交给DispatcherServlet处理。

contextConfigLocation:指定springmvc配置的加载位置,如果不指定则默认加载WEB-INF/[DispatcherServlet 的Servlet 名字]-servlet.xml。

第二步:springmvc.xml配置

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "><!-- 配置handler --><bean name="/queryBooks.action" class="cn.byr.ssm.controller.ItemsQueryController1"></bean><!-- 处理器映射器 将beanname作为url进行查找 需要在配置 handler指定beanname(就是url)-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean><!-- 处理器 适配器-->
<bean        class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
</bean></beans>

第三步 handler的开发

package cn.byr.ssm.controller;import java.util.ArrayList;
import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;import cn.byr.ssm.po.Book;public class ItemsQueryController1 implements Controller {@Overridepublic ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {//调用service来查询数据 查询商品列表返回数据 这里采用静态数据
        List<Book> books=new ArrayList<Book>();Book book1 =new Book();book1.setBname("java书籍1");book1.setAuthor("a");Book book2 =new Book();book2.setBname("java书籍2");book2.setAuthor("b");Book book3 =new Book();book3.setBname("java书籍3");book3.setAuthor("c");books.add(book3);books.add(book2);books.add(book2);//返回modelandview
        ModelAndView modelAndView = new ModelAndView();//相当于request的setAttribute方法modelAndView.addObject("books", books);//指定数据modelAndView.setViewName("/WEB-INF/jsp/book/itemsList.jsp");return modelAndView;}}

第四步  itemList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body> 
<form action="${pageContext.request.contextPath }/books/queryBooks.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr><td>商品名称</td><td>商品作者</td>
</tr>
<c:forEach items="${books}" var="book">
<tr><td>${book.bname }</td><td>${book.author}</td></tr>
</c:forEach></table>
</form>
</body></html>


第五步 测试结果

  http://localhost:8080/springmvcfirst/queryBooks.action

 

转载于:https://www.cnblogs.com/xiaoying1245970347/p/4935222.html


http://www.taodudu.cc/news/show-854201.html

相关文章:

  • 基于阿里云向量检索 Milvus 版与 PAI 搭建高效的检索增强生成(RAG)系统
  • iOS copy的正确姿势
  • 正点原子[第二期]Linux之ARM(MX6U)裸机篇学习笔记-15.4讲--ARM异常中断返回
  • 果蔬经营平台|基于SSM+vue的果蔬经营平台系统的设计与实现(源码+数据库+文档)
  • OceanBase v4.3特性解析:新功能“租户克隆”的场景与应用指南
  • EasyExcel多行表头带动态下拉框导入导出具体实现
  • mysql 必知必会 笔记
  • 项目QQ音乐开发记录
  • 【独家:震惊!——西城区所有学区优质度透解与大排名,泄密了!】
  • Git学习(一)(2015年11月12日)
  • CentOS 6.3系统安装配置KVM虚拟机
  • 谷歌验证 (Google Authenticator) 的实现原理是什么?
  • QQ与我联系
  • 【原创】erlang 模块之 epmd
  • weblit渲染过程
  • [AlwaysOn Availability Groups]CLUSTER.LOG(AG)
  • ubuntu下安装jdk、tomcat、mysql
  • linux下Oracle 10g安装(超级详细图解教程)
  • ​shell中经常能看到的:/dev/null 21
  • 平台系统表怎么修改为普通表
  • android项目闪屏页细节
  • apache Storm学习之二-基本概念介绍
  • (NO.00004)iOS实现打砖块游戏(八):游戏中小球与砖块的碰撞
  • Swift 数组、字典
  • NavigationBar 渐隐效果
  • [数据结构]合并有序数组
  • spring BeanFactory加载xml配置文件示例
  • nginx location配置详解
  • 三星1TB硬盘MM804RS不识别加电磁头打盘敲盘异响不识别开盘恢复数据
  • Zookeeper Learning
  • 浑浑噩噩工作已多年
  • log4j+logback+slf4j+commons-logging的关系与调试
  • 使用 rem 实现 适配各种屏幕布局
  • 设计模式Adapter模式的五分钟
  • oracle的SCN和Checkpoint_Change#的关系
  • A problem while linking c++ to python