近几日在研究SpringMvc,特此写一篇入门helloworld,希望能在大家前进的道路上帮助大家。结尾附赠源码。

文档参考:https://www.jb51.net/article/115553.htm

开发环境:    jdk:1.0.8、    tomcat 8.5、     Eclipse LUNA

STEP:

1.首先创建一个DynamicWebPro
SpringMvc入门系列实例下载完美运行亲测

2.导入SpringMvc需要的jar包到WebContent/WEB-INF/lib下面

SpringMvc入门系列实例下载完美运行亲测

3.配置web.xml配置文件
    (1).配置springDispatcherServlet
        <!-- 配置 Spring MVC DispatchcerServlet 前端控制器 -->
            <servlet>
                <servlet-name>springmvc</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                
                <!-- 
                    contextConfigLocation 是参数名称,该参数的值包含 Spring MVC 的配置文件路径 
                     如不使用此配置默认为/WEB-INF/ 下的 servletname-servlet.xml文件
                -->
                <!-- 
                <init-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
                </init-param>
                 -->
                <!-- 在 Web 应用启动时立即加载 Servlet -->
                <load-on-startup>1</load-on-startup>
            </servlet>

   (2).将配置servlet-mapping,将所有的请求转接到springDespatcherServlet
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <!-- 监听当前域的所有请求 -->
            <url-pattern>/</url-pattern>
        </servlet-mapping>

4.配置springMVC的配置文件
    在/WEB-INF/ 创建 servletname-servlet.xml
    文件中有代码的详细使用说明

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

    <context:annotation-config />
    <!-- 配置自动扫描的包,完成 Bean 的创建和自动依赖注入的功能 -->
    <context:component-scan
        base-package="com.test.springmvc.controller" />

    <!-- 配置视图解析器 : 如何把 handler 方法返回值解析为实际的物理视图 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans> 
5.配置响应前台请求的handler类文件
    类名之前使用注解  @Controller 将类修饰为 handler 类
    在类的方法上使用  @RequestMapping 将方法修饰为响应方法,并通过 value索引请求的后缀

注意类的包名要和上一步骤中配置自动扫描的包名一致。否则无法映射到正确的控制器

package com.test.springmvc.controller;

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

@Controller
public class tortoise {
    public static String SUCCESS = "success";

    @RequestMapping(value = "hello")
    public String hello() {
        System.out.println("hello");
        return SUCCESS;
    }
}

6.配置页面  index.jsp置于WebContent下
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
    <a href="hello">hello</a>
</body>
</html>

    配置success.jsp置于置于WebContent\WEB-INF\view下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
    <h4>SUCCESS PAGE</h4>
</body>
</html>

 

ok。至此点击运行即可。

附上资源包地址,少量积分,谢谢支持。

https://download.csdn.net/download/u012842328/11049397

相关文章:

  • 2021-11-23
  • 2021-11-29
  • 2022-01-12
猜你喜欢
  • 2021-08-09
  • 2022-02-28
  • 2021-09-13
  • 2021-04-29
  • 2021-10-08
  • 2021-11-20
  • 2022-01-08
相关资源
相似解决方案