简单的Java Spring项目示例

该文指导如何创建一个基于Maven的SpringMVC项目,包括添加Spring和Tomcat依赖,创建控制器和视图,配置Spring以及最终运行应用程序,展示首页。
摘要由CSDN通过智能技术生成
  1. 创建 Maven 项目

首先创建一个Maven项目,并添加Spring相关依赖。例如在pom.xml文件中添加以下依赖:

<dependencies>
   <!--Spring Web-->
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
   </dependency>
   <!--Spring MVC-->
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
   </dependency>
   <!--Tomcat Embed-->
   <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-core</artifactId>
      <version>${tomcat.version}</version>
   </dependency>
   <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <version>${tomcat.version}</version>
   </dependency>
   <!--JSTL-->
   <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>${jstl.version}</version>
   </dependency>
</dependencies>

 

2.创建控制器

创建一个控制器类,例如HomeController.java,代码如下:

package com.example.demo;

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

@Controller
public class HomeController {
   @RequestMapping(value = "/", method = RequestMethod.GET)
   public String home() {
      return "home.jsp";
   }
}

3.创建视图

在src/main/webapp/WEB-INF目录下创建一个名为home.jsp的视图文件,代码如下:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <title>Home Page</title>
   </head>
   <body>
      <h1>Welcome to the home page!</h1>
      <p>The time on the server is ${serverTime}</p>
   </body>
</html>

 

4.配置Spring

创建一个Spring配置文件,例如spring-servlet.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"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

   <context:component-scan base-package="com.example.demo" />

   <mvc:annotation-driven />

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

</beans>

 

 

5.运行应用

启动Tomcat服务器并运行应用,浏览器访问http://localhost:8080/应该显示出欢迎页面。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

个人练习生xx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值