• Spring MVC_Hello World


    【Hello World】

    步骤:

    (1)加入jar包,

    (2)在web.xml中配置DispatcherServlet,

    (3)加入Spring MVC的配置文件,

    (4)编写处理请求的处理器,并标识为处理器

    (5)编写视图

    (1)

    (2)

    web.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns="http://java.sun.com/xml/ns/javaee"
     4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     5     id="WebApp_ID" version="2.5">
     6 
     7     <!-- 配置DispatcherServlet -->
     8     <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
     9     <servlet>
    10         <servlet-name>springDispatcherServlet</servlet-name>
    11         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    12         <!-- 配置DispatcherServlet的一个初始化参数:配置Spring MVC配置文件的位置和名称 -->
    13         <init-param>
    14             <param-name>contextConfigLocation</param-name>
    15             <param-value>classpath:springmvc.xml</param-value>
    16         </init-param>
    17         <load-on-startup>1</load-on-startup>
    18     </servlet>
    19 
    20     <servlet-mapping>
    21         <servlet-name>springDispatcherServlet</servlet-name>
    22         <url-pattern>/</url-pattern>
    23     </servlet-mapping>
    24 </web-app>

    (3)

    springmvc.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
     7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
     9 
    10    <!-- 配置自动扫描的包 -->
    11    <context:component-scan base-package="com.hk.springmvc"></context:component-scan>
    12    
    13    <!-- 配置视图解析器:如何把handler方法返回值解析为物理视图 -->
    14    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    15         <property name="prefix" value="/WEB-INF/views/"></property>
    16         <property name="suffix" value=".jsp"></property>
    17    </bean>
    18 </beans>

    (4)

     1 package com.hk.springmvc.handlers;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 
     6 //控制器
     7 @Controller
     8 public class HelloWorld {
     9     
    10     /*
    11      * 1.使用@RequestMapping注解来映射请求的URL
    12      * 2.返回值会通过视图解析器解析为物理的视图,对于InternalResourceViewResolver 视图解析器会做如下的解析:
    13      *      prefix + returnval + 后缀,
    14      *   通过这样的方式得到实际的物理视图,然后做转发操作
    15      *   /WEB-INF/views/success.jsp
    16      */
    17     @RequestMapping("/helloworld")
    18     public String hello(){
    19         System.out.println("Hello World");
    20         return "success";
    21     }
    22 
    23 }

    (5)

    index.jsp:

     1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     2     pageEncoding="ISO-8859-1"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10    <a href="helloworld">Hello World</a>
    11 </body>
    12 </html>

    success.jsp:

     1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     2     pageEncoding="ISO-8859-1"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10    <h4>SUCCESS</h4>
    11 </body>
    12 </html>

    运行结果:

    点击“Hello World”

    附:

    每接触一个新领域,我就像一块掉进水里的海绵,四面八方的养分都让我不断充实。O(∩_∩)O~
  • 相关阅读:
    2021微软Power BI 每月功能更新系列——Power BI 6月版本功能完整解读 ​
    Power Automate实例应用
    微软Power BI 每月功能更新系列——2021年4月版本功能更新全面解读
    Power Apps 应用:构建一套简易的五一休假报备管理系统(一)
    如何在Azure上使用 Python 创建ADF
    微软Power BI 每月功能更新系列——2021年3月版本功能更新全面解读
    Power BI 制作技巧 — 按钮动画效果
    连接到Power BI Premium或 Azure Analysis Services时,如何更改Excel中的用户帐户
    在 Power BI 中连接到 SAP HANA 数据库
    2021微软Power BI 每月功能更新系列——Power BI 2月版本功能完整解读
  • 原文地址:https://www.cnblogs.com/zhzcode/p/9690388.html
Copyright © 2020-2023  润新知