简体   繁体   English

使用Spring MVC的单页应用程序

[英]Single page application with Spring MVC

I'm trying to build a single page web application using Spring MVC, relying on native Spring MVC controllers (with model and views) and common rest controllers that return JSON information. 我正在尝试使用Spring MVC构建一个单页Web应用程序,它依赖于本机Spring MVC控制器(带有模型和视图)以及返回JSON信息的通用rest控制器。

I want to briefly explain you what i'm doing with a simple example: 我想用一个简单的例子简要地向您解释我在做什么:

  1. if the user click on the home page button inside the menu, an Ajax method is called and it sends a GET HTTP request to my server that handles the request with a Spring MVC controller, returning the rendered html file of the home page, coming from a JSP file containing JSTL and Spring tags. 如果用户单击菜单内的主页按钮,则将调用Ajax方法,该方法将GET HTTP请求发送到我的服务器,该服务器使用Spring MVC控制器处理该请求,并返回呈现的主页html文件,该文件来自包含JSTL和Spring标记的JSP文件。
  2. Once the Ajax method receives the content of the homepage it changes the content of the main div inside my html body with this new one. 一旦Ajax方法接收到首页的内容,它就会用这个新的HTML主体更改html主体内的主要div的内容。 In the end what the user has, is the homepage without refreshing the page (sorry for the game of words). 最后,用户拥有的是没有刷新页面的首页(对不起文字游戏)。

The only problem is that if a user writes inside the browser the url of the GET HTTP request that the Ajax method uses to get the homepage content, right now what happens is that he will load the homepage content inside the browser(and of course i don't want him to be able to send this get http request manually). 唯一的问题是,如果用户在浏览器中写入Ajax方法用于获取首页内容的GET HTTP请求的网址,那么现在发生的事情是他将在浏览器中加载首页内容(当然,不希望他能够手动发送此get http请求)。

I really hope to have explained myself with this example and I would like to ask you if you have any suggestions to give me in this direction in order to solve this problem! 我真的希望通过这个例子向我解释一下,我想问一下您是否有任何建议可以解决这个问题! I know that using a framework such as React or Angular, things would be much much easier but i still have to learn one! 我知道使用诸如React或Angular之类的框架,事情会容易得多,但我仍然必须学习一个框架! This will be the first upgrade I'll do in the future to this webapp. 这将是我将来将对此Webapp进行的首次升级。

Thank you very much for your help! 非常感谢您的帮助!

You have to use @RestController and not @Controller(or your could do @Controller for the class and @ResponseBody for the single method).Therefore your return method should be a ResponseEntity. 您必须使用@RestController而不是@Controller(或者您可以对类使用@Controller,对单个方法可以使用@ResponseBody),因此您的return方法应该是ResponseEntity。 According with the flow of your code, you can set the HttpStatus.OK or HttpStatus.INTERNAL_SERVER_ERROR. 根据您的代码流,您可以设置HttpStatus.OK或HttpStatus.INTERNAL_SERVER_ERROR。

Try to authenticate all your HTTP call from JavaScript to your API (for example with a token like JSON Web Token). 尝试对从JavaScript到API的所有HTTP调用进行身份验证(例如,使用JSON Web令牌之类的令牌)。 If the token is not present or incorrect, return a 401 HTTP Status(Unauthorized). 如果令牌不存在或不正确,则返回401 HTTP状态(未授权)。

There is a lot of good tutorial like this one: http://www.baeldung.com/spring-security-oauth-jwt 有很多类似这样的好教程: http : //www.baeldung.com/spring-security-oauth-jwt

You just have to add an Http Header to your ajax call with the token returned by the authentication. 您只需要使用身份验证返回的令牌将Http标头添加到您的ajax调用中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM