SpringMVC学习笔记1(HelloWorld)

SpringMVC学习笔记

第一部分 HelloWorld

文件结构

SpringMVC学习笔记1(HelloWorld)_第1张图片

web.xml


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>/WEB-INF/applicationContext.xmlparam-value>
    context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <servlet>
        <servlet-name>DispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2019/4/22
  Time: 15:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$title>
  head>
  <body>
     <a href="helloworld">helloworlda><br>
     <a href="/springmvc/testRequestMapping">Test RequestMappinga>
  body>
html>

success.jsp

<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2019/4/22
  Time: 15:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>successtitle>
head>
<body>
    <h1>Hello SpringMVCh1>
body>
html>

HelloWorld.java

package com.wss.hellomvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Helloworld {

    @RequestMapping("/helloworld")
    public String hello() {
        System.out.println("helloworld");
        return "success";
    }

}

SpringMVCTest.java

package com.wss.hellomvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("springmvc")
@Controller
public class SpringMVCTest {

    public static final String SUCCESS = "success";
    @RequestMapping("/testRequestMapping")
    public String testRequestMapping() {
        System.out.println("testRequest");
        return SUCCESS;
    }

}

springmvc.xml


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

    
    <context:component-scan base-package="com.wss.hellomvc">context:component-scan>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/">property>
        <property name="suffix" value=".jsp">property>
    bean>

beans>

至此,SpringMVC的HelloWorld就已经完成了,具体怎么运行,请先百度一下(实际上配置了tomcat就基本上没什么大问题)。这里有一个小问题的解决方法,读者可以参考一下

SpringMVC学习笔记1(HelloWorld)_第2张图片

你可能感兴趣的:(Spring,MVC)