• 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>

     最后的测试结果

  • 相关阅读:
    jquery ui draggable,droppable 学习总结
    VSCode设置网页代码实时预览
    ionic3-修改APP应用图标(icon)和APP启动界面(Splash)
    Ionic3页面的生命周期
    videogular2 在ionic3项目里报错(rxjs_1.fromEvent is not a function)
    IDEA的maven项目的netty包的导入(其他jar同)
    maven的安装与项目的创建
    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
    使用二分法查询二维整型数组的值(找到返回其坐标)
    乐观锁以及悲观锁
  • 原文地址:https://www.cnblogs.com/djoker/p/6602543.html
Copyright © 2020-2023  润新知