SpringMVC | 在IDEA中创建一个SpringMVC的项目并且编写一个HelloWorld

本文指导如何在IntelliJ IDEA中创建一个SpringMVC项目,并编写首个HelloWorld应用。首先,通过选择SpringMVC和Web Application模板新建项目,接着完善项目结构,包括创建lib和classes目录。然后,配置项目结构,添加lib到模块,调整Artifacts。接下来,配置Tomcat服务器。最后,编写HelloWorld,包括修改配置文件,更新index.jsp,创建Controller类,以及处理视图解析。
摘要由CSDN通过智能技术生成

新建一个工程

  • 点击Create New Project


    13424350-14298846c529be2a.png
  • 选择SpringMVC和Web Application,然后我们因为没有下载SpringMVC的包,因此选择Download进行下载


    13424350-357336df793ab344.png
  • 给项目取名字后点击Finish完成创建

对项目结构进行完善

  • 新建一个lib和classes目录
    在WEB-INF下新建这两个目录


    13424350-a9966f1b94bf7c34.png

    其中lib来存放需要添加的jar包。

  • 在Project Structure中点击Modules修改如下东西


    13424350-e94b23440af4c6f7.png

并把我们刚刚的lib添加到:


13424350-a820f4f525c3f94b.png

然后在Artifacts中进行Fix。

  • 配置Tomcat
    添加一个Tomcat


    13424350-92a60c9ac739fdee.png

    13424350-3d94cd96b371aa4f.png
    这一步可有可无

这样既可完成对项目的部署了。

编写HelloWorld

  • 我们新建项目后打开web.xml,里面已经给我们自动生成了代码了,我们需要对代码进行改动
    对于<servlet-mapping>节点中的<url-pattern>进行修改,改为<url-pattern>/</url-pattern>,更改后的代码如下:
<?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>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  • xxx-servlet.xml默认配置文件
    这个默认配置文件的xxx取决于上面web.xml中配置的servlet的名字,与其一致,并且该文件在WEB-INF目录下,因此我们的配置文件名为:dispatcher-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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 配置自动扫描的包-->
    <context:component-scan base-package="com.cerr.springmvc.handlers"/>
    
    <!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        id="internalResourceViewResolver" >
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/views/"/>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
  • 修改index.jsp
    给其添加一个超链接。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <a href="helloworld">HelloWorld</a>
  </body>
</html>
  • 新建一个Controller类

使用@RequestMapping来映射请求的url
返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver视图解析器,会做如下的解析:
通过prefix+returnVal+后缀 的方式得到实际的物理视图,然后做转发操作

新建一个HelloWorld,并给其注解@Controller,给其方法注解上@RequestMapping(),里面填入要映射请求的url,例如前面<a href="helloworld">HelloWorld</a>,则在此处注解为:@RequestMapping("/helloworld")。对于我们的配置:得到的是/WEB-INF/views/+success +.jsp 即 /WEB-INF/views/success.jsp,因此我们在WEB-INF目录下新建一个views目录,然后新建一个success.jsp文件。

package com.cerr.springmvc.handlers;

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

@Controller
public class HelloWorld {

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

  • 转发目的jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h4>hhh</h4>
</body>
</html>

这样helloworld即编写好了,直接运行就行。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值