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

     最后的测试结果

  • 相关阅读:
    How to Install Linux, Apache, MySQL, PHP (LAMP) stack on CentOS 6 【Reliable】
    可以把一些常用的方法,写入js文件,引入html界面
    把功能写在方法里,函数化,方法化
    那些SQL语句
    Linux&shell之高级Shell脚本编程-创建菜单
    Linux&shell之高级Shell脚本编程-创建函数
    PHP isset()与empty()的使用区别详解
    如何打开mo文件并修改 PoEdit
    Linux&shell之如何控制脚本
    Linux&shell之显示数据
  • 原文地址:https://www.cnblogs.com/djoker/p/6602543.html
Copyright © 2020-2023  润新知