欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

hualinux springMVC 5.2:使用 idea建立简单springMVC

程序员文章站 2023-11-05 11:13:46
目录一、环境说明1.1 使用环境1.2 下载tomcat二、建立springMVC项目2.1创建SpringMVC项目2.2移动lib目录2.3.1 把项目中的lib目录移到WEB-INF下2.3.2 修改项目结构2.4添加对maven的支持2.4.1 添加maven框架2.4.2 pom.xml设置maven2.4.3 整体项目目录结构2.5 设置tomcat编码三、用SpringMVC建立一个简单helloWorld3.1编写基于模板......

 目录

一、环境说明

1.1 使用环境

1.2 下载tomcat

二、建立springMVC项目

2.1 创建SpringMVC项目

2.2 移动lib目录

2.3.1 把项目中的lib目录移到WEB-INF下

2.3.2 修改项目结构

2.4 添加对maven的支持

2.4.1 添加maven框架

2.4.2 pom.xml设置maven

2.4.3 整体项目目录结构

2.5 设置tomcat编码

三、用SpringMVC建立一个简单helloWorld

3.1 编写基于模板的hello

3.1.1 配置web.xml

3.1.2 编写Hello代码

3.1.3 配置applicationContext.xml

3.1.4 配置dispatcher-servlet.xml

3.1.5 编写登陆成功后视图jsp代码

3.1.6 编写首页index.jsp代码

3.1.7 运行tomcat

3.2 编写直接返回数据的hello

3.2.1 修改Hello代码

3.2.3 以json方式显示


本章将讲如何使用idea建立简单的springMVC项目

一、环境说明

1.1 使用环境

名称

版本

安装方式

备注

系统

win10

-

64位,旗舰版

jdk

14.0.1

exe

java开发工具目前最高最新版本

idea

2020.1

exe

java最著名的ide工具

具体安装见:《hualinux java 1.5:java开发工具idea 2020安装及配置

因为要使用到web在此基础上还需要安装java的web,我这里选择tomcat,使用版本为最新稳定的tomcat9,目前最新版本为9.0.36

1.2 下载tomcat

打开tomcat9的下载页,我这里下载的是二进制核心版本,免安装的,因为我的操作系统是64位,所以选择64,如下图所示:

hualinux springMVC 5.2:使用 idea建立简单springMVC

下载解压apache-tomcat-9.0.36-windows-x64.zip到D:\Programs目录中,命令为tomcat-9.0.36,如下图所示:

hualinux springMVC 5.2:使用 idea建立简单springMVC

先放在这里不动,等到添加到idea中就行了

 

二、建立springMVC项目

2.1 创建SpringMVC项目

如果你之前有建立过项目的话,先关闭项目(File-->Close Project)

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

2.2 移动lib目录

根据tomcat9官网说明文档对目录的要求:

hualinux springMVC 5.2:使用 idea建立简单springMVC

我上图中,知道lib文件是放在WEB-INF目录下的

WEB-INF是安全目录不对外开放的,有的初学者把html放在WEB-INF目录下,结果老是访问不到

2.3.1 把项目中的lib目录移到WEB-INF下

把项目下的lib目录剪切并粘贴在WEB-INF目录下

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

 

2.3.2 修改项目结构

hualinux springMVC 5.2:使用 idea建立简单springMVC

 hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

同理,也做一个另一个目录的操作,就是WIN-INF/lib目录下除了上面2个jar包不选,其它都添加

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

 

2.4 添加对maven的支持

如果不懂的话可以看《hualinux spring 4.15:spring添加maven支持 10分钟学会mavne(新手必看)

2.4.1 添加maven框架

右击项目名-->Add Framework Support...

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

2.4.2 pom.xml设置maven

#1 设置groupId

我这里设置为com.hualinux

#2 指定java版本

我的java版本为14,所以指定为14

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>14.0.1</source>
                    <target>14.0.1</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

总的pom.xml配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hualinux</groupId>
    <artifactId>myspringmvc</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <dependencies>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>14.0.1</source>
                    <target>14.0.1</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
</project>

导入maven改变

hualinux springMVC 5.2:使用 idea建立简单springMVC

2.4.3 整体项目目录结构

做完上面的修改后,整体的项目目录结构变成如下这样

hualinux springMVC 5.2:使用 idea建立简单springMVC

2.5 设置tomcat编码

如果不设置tomca编码,有会输出会乱码,所以要设置一下,可以看我的另一篇文章《hualinux servlet 2.7:IDEA 2020.1 x64 控制台日志乱码问题》,其它几项之前我已经设置过了,现在只需要设置tomcat即可

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

 

三、用SpringMVC建立一个简单helloWorld

3.1 编写基于模板的hello

3.1.1 配置web.xml

打开web-->WEB-INF-->web.xml,把

<url-pattern>*.form</url-pattern>

修改为

<url-pattern>/</url-pattern>

hualinux springMVC 5.2:使用 idea建立简单springMVC

ps:<url-pattern>/</url-pattern>和<url-pattern>/*</url-pattern> 区别请看《Spring MVC的困惑url-pattern /和/*的区别

3.1.2 编写Hello代码

在src-->main-->java-->com.hualinux.srpingmvc.handers.Hello.java

package com.hualinux.srpingmvc.handers;

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

//@Controller表示要使用模板,如果用@RestController则表示直接把数据返回给浏览器
@Controller
public class Hello {
    /**设置路由映射,意思是当访问路径为/hello时则交给此函数处理
     * 因类上面添加的是@Controller所以返回值为模板路径下的名字,
     * 这里返回的是success,所以模板的名字为success.jsp
     */
    @RequestMapping("/hello")
    public String sayHi(){
        System.out.println("你好,springMVC");
        return "success";
    }

}

hualinux springMVC 5.2:使用 idea建立简单springMVC 

ps:因为上面代码使用的是 @Controller 注解,返回的是模板名,并不是字符串!

上面例子中是返回一个名为 success 的模板

●Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请求

●在控制器的类定义及方法定义处都可标注

  • 类定义处:提供初步的请求映射信息。相对于 WEB 应用的根目录
  • 方法处:提供进一步的细分映射信息。相对于类定义处的 URL。若类定义处未标注 @RequestMapping,则方法处标记的 URL 相对于WEB 应用的根目录

●DispatcherServlet 截获请求后,就通过控制器上@RequestMapping 提供的映射信息确定请求所对应的处理方法。

hualinux springMVC 5.2:使用 idea建立简单springMVC

3.1.3 配置applicationContext.xml

web-->WEB-INF-->ApplicationContext.xml是spring 全局配置文件,用来控制spring 特性的,默认为空

暂时不用写

 

3.1.4 配置dispatcher-servlet.xml

web-->WEB-INF-->dispatcher-servlet.xml 是spring mvc里面的,控制器、拦截uri转发view,默认为空

打开它添加如下内容:

<?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">

    <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
    <context:component-scan base-package="com.hualinux.srpingmvc"/>

    <!-- 让Spring MVC不处理静态资源 -->
    <mvc:default-servlet-handler />

    <!--支持mvc注解驱动,在spring中一般采用@RequestMapping注解来完成映射关系,要想使@RequestMapping注解生效
    必须向上下文中注册DefaultAnnotationHandlerMapping和一个AnnotationMethodHandlerAdapter实例
    这两个实例分别在类级别和方法级别处理。而annotation-driven配置帮助我们自动完成上述两个实例的注入。
    -->
    <mvc:annotation-driven />

    <!-- 配置视图解析器:视图名称解析器:将视图逻辑名解析为: /WEB-INF/view/<viewName>.jsp-->
    <!-- 把 handler 方法返回值解析为实际的物理视图 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>


</beans>

3.1.5 编写登陆成功后视图jsp代码

建立一个 web-->WEB-INF-->view-->success.jsp文件,其代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<html>
<head>
    <title>view视图下的jsp</title>
    <meta charset="UTF-8">
</head>
<body>
<h2>hello world!</h2>
<h2>你好,世界!</h2>
</body>
</html>

hualinux springMVC 5.2:使用 idea建立简单springMVC

3.1.6 编写首页index.jsp代码

打开web-->index.jsp,添加一个链接,代码如下:

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
  <title>首页</title>
  <meta charset="UTF-8">
</head>
<body>
<a href="hello">Hello</a>
</body>
</html>

hualinux springMVC 5.2:使用 idea建立简单springMVC

 

3.1.7 运行tomcat

默认已经配置好tomcat了,直接点运行就行了

注意:tomcat是idea运行的,不需要你先运行!有的人自作聪明,先手工运行tomcat,再在idea运行tomcat

结果觉得idea运行tomcat老报错,运行不起来

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

我再看一下idea正下方的日志输出,正常,没有中文乱码

hualinux springMVC 5.2:使用 idea建立简单springMVC

 

3.2 编写直接返回数据的hello

从上面的例子可以看出 ,这里使用的是jsp模板,在模板中显示数据,如有需要的话还可以把数据传给模板。

我们知道现在的网站基本的都是前后端分离,为什么呢?因为终端设备太多,如PC、手机、pad、墙上电视,我们一般是返回json数据给前端,让前端自行处理。

3.2.1 修改Hello代码

我们先简单点直接返回数据,其实很简单,把上面的HelloWorld.java的@Controller注解改为@RestController就行了,如下

hualinux springMVC 5.2:使用 idea建立简单springMVC

再次运行一下tomcat,

hualinux springMVC 5.2:使用 idea建立简单springMVC

点hello后发现不是转到模板,而是直接输出 sucess了

hualinux springMVC 5.2:使用 idea建立简单springMVC

3.2.3 以json方式显示

我们可以直接返回一个json格式的字符串给浏览器,当然我们也可以使用json相关的工具,如淘宝的fastjson,可以把Map、list、及其它对应转为json或json字符串

com.hualinux.srpingmvc.handers.Hello.java代码修改如下:

package com.hualinux.srpingmvc.handers;

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

//@Controller表示要使用模板,如果用@RestController则表示直接把数据返回给浏览器
// @Controller
@RestController
public class Hello {
    /**设置路由映射,意思是当访问路径为/hello时则交给此函数处理
     * 因类上面添加的是@Controller所以返回值为模板路径下的名字,
     * 这里返回的是success,所以模板的名字为success.jsp
     */
    @RequestMapping("/hello")
    public String sayHi(){
        return  "{ \"name\":\"hua\" }";
    }
}

运行tomcat,点hello效果如下:

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

hualinux springMVC 5.2:使用 idea建立简单springMVC

本文地址:https://blog.csdn.net/hualinux/article/details/107071432