java Spring MVC 和 Angularjs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16617381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:28:53  来源:igfitidea点击:

Spring MVC and Angularjs

javamavenspring-mvcangularjs

提问by Gerard Ribas

Currently I′m trying to learn Angular JS, but firstly I want to setup my environment with Spring mvc.

目前我正在尝试学习Angular JS,但首先我想用Spring mvc.

At the moment I only want to work with rest, but I have a doubt for what is the best way to place the resoucres in Spring MVC

目前我只想休息工作,但我怀疑放置资源的最佳方法是什么 Spring MVC

My simple applicaction has this squeleton:

我的简单应用程序有这个 squeleton:

my-simple-app:
 src
   main
     java
     resources
   webapp
     resources
     WEB-INF

If I want to put the appfolder from the angular-seed, what is the best place to put it?

如果我想appangular-seed 中放置文件夹,那么放置它的最佳位置是什么?

I tried to put in src/webapp/resources/appbut then I have to move the htmlfiles to WEB-INF?

我试图放入src/webapp/resources/app但后来我必须将html文件移动到 WEB-INF?

How was your skeleton in your angular-js spring mvc applications?

您在 angular-js spring mvc 应用程序中的骨架如何?

What is the best way to do the redirect to the app/index.html? to the welcome file and then work only with angularjs$routeproviders?

重定向到 的最佳方法是什么app/index.html?到欢迎文件,然后只使用angularjs$routeproviders?

Thanks!

谢谢!

回答by Abilash

As far as I can understand, your Front End technology is Angualar JSand your Back End technology is Spring MVC.

据我所知,您的前端技术是Angualar JS,您的后端技术是Spring MVC

I'm a Front End developer and hence I can provide you the advice on the structure of your HTML, CSS, and Javascript.

我是一名前端开发人员,因此我可以为您提供有关 HTML、CSS 和 Javascript 结构的建议。

Here are my recommendations:

以下是我的建议:

  1. Mode Of Communication Between Front End and Back End: JSON (Should be strictly followed for MVC Pattern)

  2. File Location: All your Front End files should be in WEB-INF folder with this structure:

    • WEB-INF/Assets: All your JavaScript Files, JavaScript Libraries, Images, CSS etc. Should Be Places Here. You Can Open A Separate Folder Each Resource Type Inside Assets

    • WEB-INF/JSP: All Your JSPs should be placed here. As Angular's greatest strength is Single page Application, you can create one JSP per main page and place them here

    • WEB-INF/HTML: All the static resources that would be injected into the JSPs using <ng-view>or <ng-include>can be placed here

  1. 前后端通信方式:JSON(MVC模式应严格遵守)

  2. 文件位置:您的所有前端文件都应位于具有以下结构的 WEB-INF 文件夹中:

    • WEB-INF/Assets:您所有的 JavaScript 文件、JavaScript 库、图像、CSS 等都应该放在此处。您可以在 Assets 中为每种资源类型打开一个单独的文件夹

    • WEB-INF/JSP: 你所有的 JSP 都应该放在这里。由于 Angular 最大的优势是单页应用程序,您可以为每个主页面创建一个 JSP 并将它们放置在这里

    • WEB-INF/HTML: 所有会被注入到 JSP 中的静态资源<ng-view>或者<ng-include>可以放置在这里

Hope this helps!

希望这可以帮助!

回答by scothis

The easiest way to get going is to take all the files in the angular-seed/appdirectory and copy them into your src/main/webappdirectory. After copying these files, you should be able to redeploy the app and have a running sample.

最简单的方法是获取angular-seed/app目录中的所有文件并将它们复制到您的src/main/webapp目录中。复制这些文件后,您应该能够重新部署应用程序并运行示例。

Most servlet containers will include index.html as a default welcome file. If yours doesn't, you can add that config in web.xml <welcome-file-list>.

大多数 servlet 容器将包含 index.html 作为默认欢迎文件。如果您没有,您可以在 web.xml 中添加该配置<welcome-file-list>

The WEB-INF directory is for web resources that should not be exposed directly to the web. web.xml is a example of a file that should not be exposed to remote users. In this case, it's safe to expose all of the app's resources directly to the web; thus you don't need to place the resources under WEB-INF.

WEB-INF 目录用于不应直接暴露给网络的网络资源。web.xml 是一个不应向远程用户公开的文件示例。在这种情况下,将应用程序的所有资源直接暴露给网络是安全的;因此您不需要将资源放在 WEB-INF 下。

回答by uzor

I recommend this structure for your project:

我为您的项目推荐这种结构:

my-simple-app:
    src
        main
            java
                controller
                    MySpringCtr.java
                models
                    Person.java
                    House.java
                    *.java
        webapp
            WEB-INF
                resources
                    css
                        style.css
                        *.css
                    js
                        angularCtr.js
                        *.js
                pages
                    index.jsp
                    *.jsp
                mvc-dispatcher-servlet.xml
                web.xml

I found a tutorial for beginners which explains step by step how to combine SpringMVC and AngularJS, you can find the tutorial and the complete code in this blogI hope it will be useful :)

我找到了一个初学者教程,它一步一步地解释了如何结合 SpringMVC 和 AngularJS,你可以在这个博客中找到教程和完整的代码希望对你有用:)