SpringMVC(2)-注解开发MVC


项目目录

一.创建maven项目,添加web支持,在pom.xml问价引入一下代码

    <build>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>
    build>

二..web模块部分的代码与上一小节的相同,新建jsp文件,配置web.xml

  1.web.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
         version="4.0">
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:annoSpring-mvc.xmlparam-value>
    context-param>
    
    <servlet>
        <servlet-name>Spring-mvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:annoSpring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>Spring-mvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
web-app>

  2.jsp代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
    ${msg}
body>
html>

三.在Java文件夹下创建com.xiaoma.controller.HelloControler类文件,代码如下

package com.xiaoma.controller;

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

/**
 * Author:xiaoma
 * FileName:HelloController
 * Data:2022/4/27 9:35
 */
@Controller
public class HelloController {
    @RequestMapping("hello")
    public String sayHello(Model model){
        model.addAttribute("msg","hello");
        //web-inf/jsp/hello.jsp
        return "hello";
    }
}

四.在resources文件夹下创建annoSpring-mvc.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.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    
    <context:component-scan base-package="com.xiaoma.controller"/>

    
    
    <mvc:default-servlet-handler />
    
    <mvc:annotation-driven />
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        
        <property name="prefix" value="/WEB-INF/jsp/" />
        
        <property name="suffix" value=".jsp" />
    bean>
beans>

五.配置并启动tomcat,浏览器访问localhost:8080/hello,结果界面上显示“hello”字样

注意1:tomcat的Deployment的Application context路径为/

注意2:记得点击idea菜单栏的File-》Project Structure-》在Output Layout中添加lib文件夹并添加所有依赖,否则会报404或者500的错误