SpringMVC学习笔记一:采用注解式搭建简单springMVC环境

搭建的环境使用的是maven项目

项目目录树:

搭建环境使用的jar包,pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>springMVC01</groupId>
  <artifactId>springMVC01</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springMVC01 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.2.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>springMVC01</finalName>
  </build>
</project>

配置项目的web.xml文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

<!-- 注册字符集Filter -->
<filter>
    <filter-name>characterEncodingFiter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>GBK</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFiter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 注册springMVC中央调度器 -->
<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

配置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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 扫描注解 -->
    <context:component-scan base-package="com.orange.controller" />    

</beans>

TestController实现类

package com.orange.controller;

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

@Controller
@RequestMapping("/test") //命名空间,表示该类型所有的方法都在改命名空间下
public class TestController {

    @RequestMapping("/mytest") //映射到/mytest访问路径执行该方法
    public ModelAndView doTest(ModelAndView mav){
        mav.addObject("message", "Hello SpringMVC! This is Test!");
        mav.setViewName("/success.jsp");
        return mav;
    }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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=GBK">
<base href="<%=basePath %>">
<title>Default Page</title>
</head>
<body>
    <a href="test/mytest">测试</a>
</body>
</html>

跳转后的success.jsp

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!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=GBK">
<base href="<%=basePath %>">
<title>Default Page</title>
</head>
<body>
<c:out value="${message }"></c:out>
</body>
</html>

最后的测试结果

时间: 2024-07-28 18:49:07

SpringMVC学习笔记一:采用注解式搭建简单springMVC环境的相关文章

Android学习笔记(二):搭建安卓开发环境

① 下载 JDK 5 or JDK 6 (JRE alone is not sufficient) ->安装->设置环境变量JAVA_HOME CLASSPATH path 下载地址:Download JDK ② 下载 Eclipse 3.3 (Europa), 3.4 (Ganymede) IDE for JAVA-> 解压 下载地址:Eclipse for JAVA developer ③ 下载 Android SDK 解压-> path 里加入 SDK 包中的 tools 目

springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定

springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需求 表现层实现 list绑定 需求 表现层实现 map绑定 本文主要介绍注解开发的集合类型參数绑定,包含数组绑定,list绑定以及map绑定 数组绑定 需求 商品批量删除,用户在页面选择多个商品.批量删除. 表现层实现 关键:将页面选择(多选)的商品id,传到controller方法的形參,方法形參使用数组接收页面请求的多个商

springmvc学习笔记(11)-springmvc注解开发之简单参数绑定

springmvc学习笔记(11)-springmvc注解开发之简单参数绑定 springmvc学习笔记11-springmvc注解开发之简单参数绑定 spring参数绑定过程 默认支持的类型 简单类型 pojo绑定 自定义参数绑定实现日期类型绑定 springmvc和struts2的区别 本文主要介绍注解开发的简单参数绑定,包括简单类型.简单pojo以及自定义绑定实现类型转换 spring参数绑定过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到contro

springmvc学习笔记(13)-springmvc注解开发之集合类型参数绑定

springmvc学习笔记(13)-springmvc注解开发之集合类型参数绑定 springmvc学习笔记13-springmvc注解开发之集合类型参数绑定 数组绑定 需求 表现层实现 list绑定 需求 表现层实现 map绑定 本文主要介绍注解开发的集合类型参数绑定,包括数组绑定,list绑定以及map绑定 数组绑定 需求 商品批量删除,用户在页面选择多个商品,批量删除. 表现层实现 关键:将页面选择(多选)的商品id,传到controller方法的形参,方法形参使用数组接收页面请求的多个商

Hibernate学习笔记:第一个程序的搭建

Hibernate学习笔记:第一个程序的搭建 前一段时间对Struts2这个框架有了一点点地了解,很高兴,自己开始学习Hibernate这个框架了.本篇博文将记录下第一个Hibernate程序的搭建过程.其实有时候个人觉得无论我们学习什么语言也好,还是学习什么框架也好,第一个HelloWorld程序真的相当重要,假如 我们在学习第一个HelloWorld程序都跑不出来,这完全影响着我们对新接触的东西的兴趣和动力,但是,往往第一个程序都会涉及到很多的配置,因此使得对于初学者要摸索一定的时间,对于我

springmvc学习笔记---面向移动端支持REST API

前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下springmvc对rest api的支持程度, 以及需要做的工作评估. 样例设计和准备: springmvc学习笔记系列的文章目录: • idea创建springmvc项目 REST API的设计原则遵循之前的博文来实现 • 移动互联网实战--Web Restful API设计和基础架构  初步设计一个查询系

SpringMVC学习笔记(二): 日常使用功能

前提: 1.web.xml 和spring-mvc核心配置如:SpringMVC学习笔记(一): 基础知识中注解实现. 2.类的@RequestMapping(value="/annotationController") 3.spring-mvc 推荐使用注解实现. 一.数据的接收 (一)URL参数数据的接收 1.使用 HttpServletRequest 获取参数 <span style="font-size:18px;"><span style

史上最全的SpringMVC学习笔记

SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 <!--configure the setting of springmvcDispatcherServlet and configure the mapping--> <servlet> <servlet-name>springmvc</servlet

springmvc学习笔记--REST API的异常处理

前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出异常, 返回的内容是500+的错误页面, 而不是json内容, 这让移动端的调用方很难处理. 本文主要讲述对于rest api, springmvc对异常的解决处理方案. 系列整理: springmvc学习笔记系列的文章目录: • idea创建springmvc项目 • 面向移动端的REST API