您好intellij 2020 springmvc world 1

Image for post
IntelliJ IDEA 2020.1
IntelliJ IDEA 2020.1

I have just spent the last 3 days wrestling with the Spring MVC configuration trying to understand how to get a simple Hello World index page to show up. Going through this process makes me truly appreciate the simplicity of other frameworks, such as Express.js. This is also the reason why abstractions such as Spring Boot have been created on top of the Spring Framework, in an attempt to remove the configuration nightmares that come about with xml config files, dependency management, and even IntelliJ plugin configuration.

最近3天,我一直在努力尝试使用Spring MVC配置,以了解如何显示简单的Hello World索引页面。 通过此过程使我真正欣赏到其他框架(如Express.js)的简单性。 这也是为什么在Spring Framework之上创建诸如Spring Boot之类的抽象的原因,以消除xml配置文件,依赖项管理甚至IntelliJ插件配置带来的配置噩梦。

However, oftentimes, in the midst of the struggle, that is where deeper learning takes place.

但是,通常在斗争中,这是进行深入学习的地方。

I have found a lot of tutorials and how-to guides using Eclipse to set this up, which is why I was determined to get it running in my favorite java IDE, IntelliJ.

我发现了很多使用Eclipse进行设置的教程和操作指南,这就是为什么我决心让它在我最喜欢的Java IDE IntelliJ中运行。

At any rate, here are the steps to setting up a Spring MVC App in IntelliJ.

无论如何,这是在IntelliJ中设置Spring MVC App的步骤。

  • Download and Install IntelliJ 2020.1 Ultimate Edition. They offer a free trial, but if you’re a student, you may be able to grab your self a free copy. Ultimate edition is required to build Web Apps in Java, so the community edition will not cut it for a Spring MVC app.

    下载并安装IntelliJ 2020.1 Ultimate Edition 。 他们提供免费试用,但是如果您是学生,则可以免费获取自己的副本 。 最终版本是用Java构建Web应用程序所必需的,因此社区版本不会将其用于Spring MVC应用程序。

  • If you haven’t already, make sure you install the JDK (Java Development Kit). I recommend the JDK 11, which is the latest version that has LTS (Long Term Support). I personally use an Open Source JDK flavor by AWS, Amazon Coretto 11.

    如果尚未安装,请确保已安装JDK(Java开发工具包)。 我建议使用JDK 11 ,它是具有LTS(长期支持)的最新版本。 我个人使用了AWS的开放源JDK风格的Amazon Coretto 11

  • Open up IntelliJ and make sure the Spring plugins are enabled. To get to plugins from the home screen, click configure => plugins:

    打开IntelliJ并确保启用了Spring插件。 要从主屏幕访问插件,请单击配置=>插件:
Image for post
intelliJ plugins
intelliJ插件
  • Download Apache Tomcat 9. It is an Application Server that is required for the Spring MVC to run.Make sure to download Apache Tomcat Core. Unzip the download to a location of your choice.

    下载Apache Tomcat 9 。 它是Spring MVC运行所必需的应用服务器。请确保下载Apache Tomcat Core。 将下载文件解压缩到您选择的位置。

  • Give full access to Apache Tomcat. You can do this by navigating to the folder in a CLI and changing the permissions.

    授予对Apache Tomcat的完全访问权限。 您可以通过在CLI中导航到该文件夹​​并更改权限来完成此操作

Image for post
linux permissions
linux权限

Now the initial housekeeping is done, you will want to create a new project.

现在,初始整理工作已经完成,您将要创建一个新项目。

  1. Create a Spring MVC project. You will need to select Spring MVC, Java EE Web Application, and Java EE Application Server. For the Application Server, you may have to add Apache Tomcat as a new Server that IntelliJ can recognize.

    创建一个Spring MVC项目。 您将需要选择Spring MVC,Java EE Web应用程序和Java EE应用程序服务器。 对于Application Server,您可能必须将Apache Tomcat添加为IntelliJ可以识别的新服务器。

Image for post

If all goes well, you should see an initial project structure like as follows :

如果一切顺利,您应该会看到一个初始项目结构,如下所示:

Image for post
2
2

2. Add the Spring MVC jar files to the project’s “artifact.”

2.将Spring MVC jar文件添加到项目的“工件”中。

This step caused me days of headache and struggle. Go to file => project structure, and click on artifacts. Then you may see a message towards the bottom :

这一步让我头疼又挣扎。 转到文件=>项目结构,然后单击工件。 然后,您可能会在底部看到一条消息:

Image for post

Click fix and apply to add the jar’s to your project module.

单击修复并应用以将罐子添加到您的项目模块中。

3. create a “base package”. This will be the primary package where your project is located. You should place the package in the src folder that was automatically created by IntelliJ.

3.创建一个“基本包”。 这将是您的项目所在的主要软件包。 您应该将包放在IntelliJ自动创建的src文件夹中。

Image for post

4. Configure the web.xml file. It is stored in the WEB-INF folder. The only change you should have to make is the urlPattern of the dispatcher servlet.

4.配置web.xml文件。 它存储在WEB-INF文件夹中。 您唯一要做的更改是调度程序servlet的urlPattern。

Image for post

the url-pattern should just have a simple forward slash as it’s value.

url-pattern应该只包含一个简单的正斜杠,因为它是有价值的。

To be thorough, here is a screen shot of the complete web.xml file, generated automatically by IntelliJ :

确切地说,这是IntelliJ自动生成的完整web.xml文件的屏幕截图:

Image for post
web.xml
web.xml

5. Configure the dispatcher-servlet.xml file, also stored in the WEB-INF folder.

5.配置dispatcher-servlet.xml文件,该文件也存储在WEB-INF文件夹中。

  • Add support for component scanning

    添加对组件扫描的支持
Image for post

this allows the spring framework to scan all classes with a @Component annotation and to treat them as “spring beans”. Note the base package is set to the package we created.

这允许spring框架使用@Component注释扫描所有类,并将它们视为“ spring bean”。 请注意,基本软件包已设置为我们创建的软件包。

  • Add support for form conversion, formatting, and validation

    添加对表单转换,格式设置和验证的支持
Image for post

Adding the above <mvc/> tag causes IntelliJ to generate a few more urls in the <beans> header tag.

添加上面的<mvc />标记会使IntelliJ在<beans>标头标记中生成更多URL。

This is the most cumbersome step and tricky step. You need to manually change every occurence of “cache” to “tx”. Don’t ask me why Spring MVC or IntelliJ doesn’t do this automatically. The Spring MVC will have a bunch of urls in the <beans> header tag to URLS having the word “cache” in them. But the correct dependency required is the spring-tx jar file that should have been downloaded by IntelliJ if you selected the Spring MVC project.

这是最麻烦和棘手的步骤。 您需要手动将每次出现的“缓存”更改为“ tx”。 不要问我为什么Spring MVC或IntelliJ不会自动执行此操作。 Spring MVC在<beans>标头标记中将有一堆URL,这些URL中包含单词“ cache”。 但是,如果选择了Spring MVC项目,则所需的正确依赖关系是IntelliJ应该已经下载的spring-tx jar文件。

Image for post

After you finish changing out the word “cache”, you will end up with the following <beans> header tag info for the dispatcher-servlet.xml file

完成更改“高速缓存”一词后,您将得到以下有关dispatcher-servlet.xml文件的<beans>标头标记信息

Image for post
  • Add a bean to resolve the view

    添加一个bean来解析视图
Image for post

For the prefix, that is the path to our front end views (html , jsp, thymeleaf, etc.). Don’t forget the forward slash at the end of views/ because the prefix will not consider views to be a folder name, but part of the file name instead.

对于前缀,这是我们前端视图的路径(html,jsp, thymeleaf等)。 不要忘记views /末尾的正斜杠,因为前缀不会将view视为文件夹名称,而是文件名的一部分。

In this example, we want our view to be a .jsp file type. JSP’s are (java server pages) an html templating system for java apps.

在此示例中,我们希望视图为.jsp文件类型。 JSP是( Java服务器页面 )用于Java应用程序的html模板系统。

To be complete, here is the completed dispatcher-servlet.xml file :

为了完整起见,这是完整的dispatcher-servlet.xml文件:

Image for post
dispatcher-servlet.xml
dispatcher-servlet.xml

6. Set up the View

6.设置视图

6.1 — Create the views folder in WEB-INF/views. Move the index.jsp file into the newly created folder.

6.1 —在WEB-INF / views中创建views文件夹。 将index.jsp文件移动到新创建的文件夹中。

Image for post

6.2 — Create a basic html page in index.jsp

6.2 —在index.jsp中创建一个基本的html页面

Image for post

7. Create a Spring Controller

7.创建一个Spring Controller

  • create a java class in the base package. Call the class some type of controller.

    在基本包中创建一个Java类。 将该类称为某种类型的控制器。
  • Add the annotation to the class to make it a @Controller.

    将注释添加到类中,使其成为@Controller。
  • Create a method in the class that returns a String. the string that is returned MUST be the name of the .jsp view file you just created. In this case, it should be index, since we just moved the index.jsp file that was generated by intelliJ.

    在类中创建一个返回String的方法。 返回的字符串必须是您刚刚创建的.jsp视图文件的名称。 在这种情况下,它应该是索引,因为我们只是移动了intelliJ生成的index.jsp文件。
  • Add the @RequestMapping(“/”) annotation to the method. Set the path to be a simple forward slash

    将@RequestMapping(“ /”)批注添加到方法中。 将路径设置为简单的正斜杠
Image for post

The final step is to run the app! Click the run button next to the Apache Tomcat configuration menu.

最后一步是运行该应用程序! 单击Apache Tomcat配置菜单旁边的运行按钮。

Image for post
Application Server Run — IntelliJ
应用程序服务器运行-IntelliJ

If all goes well, you should see a browser pop up with the contents of the index.jsp file showing to the page.

如果一切顺利,您应该会看到一个浏览器弹出,其中index.jsp文件的内容显示在页面上。

I can’t imagine how this makes the life of a java developer any easier. But, all this configuration could have been the motivation behind why Spring Boot came into existence and further abstracted away these complicated configurations.

我无法想象这如何使Java开发人员的生活变得更轻松。 但是,所有这些配置可能是Spring Boot出现并进一步抽象化这些复杂配置的背后动机。

翻译自: https://medium.com/swlh/hello-springmvc-world-in-intellij-2020-1-b65802d61ac4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值