zl程序教程

您现在的位置是:首页 >  后端

当前栏目

在Mac系统下用STS搭建一个Spring MVC项目

Spring项目系统MacMVC 一个 搭建 下用
2023-09-27 14:26:55 时间

[本文出自天外归云的博客园]

从STS的下载到空项目的搭建

1. 下载STS,下载解压缩后点击sts-bundle文件夹中的STS文件启动ide;
2. 创建Spring MVC项目:File->New->Dynamic Web Project->输入Project name;
3. 添加Spring MVC所需要的jar包(网上下载,百度搜:“Spring MVC jar包下载”):把所有的jar包都添加到WebContent/WEB-INF/lib目录下(lib文件夹手动创建);
4. 添加web.xml文件:在WebContent/WEB-INF目录下创建一个叫“web.xml”的文件;
5. 添加对应Servlet的xml文件:在Java Resources/src目录点击右键->New->Other->Spring Bean Configuration File->勾选“aop/beans/context/mvc”这四个->Finish(本章命名为“springmvc-servlet.xml”)。
至此Spring MVC空项目搭建完毕,接下来就是填充项目了。

*STS代码自动提示功能

1. Java代码自动补全:“Sprint Tool Suite->偏好设置->Java->Editor->Content Assist->Auto Activation->Enable auto activation->Auto activation triggers for Java:<=:.abcdefghijklmnopqrstuvwxyz(,”;
2. XML/HTML代码自动补全:把步骤1中的Java换成XML/HTML即可,方法相同。

项目填充

1.添加Controller文件(src/controller/HelloController.java):
package controller;

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

@Controller
public class HelloController {  
    @RequestMapping(value="/",method = RequestMethod.GET)  
    public String home(ModelMap model) {  
        model.addAttribute("message", "This is the homepage.");  
        return "index";
    }
}  
2. 添加View文件(WEB-INF/views/index.jsp):
<%@ 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>主页面</title>
</head>
<body>
    <h1>Message : ${message}</h1>
</body>
</html>
3. 填充Servlet的xml文件(src/springmvc-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:aop="http://www.springframework.org/schema/aop"
    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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    <mvc:annotation-driven/>
    <context:component-scan base-package="controller"></context:component-scan>  
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>  
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>  
    <bean  
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="WEB-INF/views/"/> 
        <property name="suffix" value=".jsp"/>  
    </bean>    
</beans>
4. 填充“web.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:aop="http://www.springframework.org/schema/aop"
    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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    <mvc:annotation-driven/>
    <context:component-scan base-package="controller"></context:component-scan>  
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>  
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>  
    <bean  
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="WEB-INF/views/"/> 
        <property name="suffix" value=".jsp"/>  
    </bean> 
</beans>
5. 启动服务器,并访问homepage: