P
P
parkito2016-10-01 02:34:50
JavaScript
parkito, 2016-10-01 02:34:50

How to connect spring and ajax?

Hello. Help, please, to solve a problem.
Task: The page sends an ajax request to a servlet. The servlet sends the result of some action back to the page. The page on the page displays this or that information, which depends on the result on the servlet.
Before I started working with spring in one of the cases, the servlet sent

} else {
                resp.setStatus(530);
            }


This was handled in ajax
<script>
                                    function unable(par1, par2) {
                                        popBox();
                                        function popBox() {
                                            x = confirm('Are you sure? ');
                                            if (x == true) {
                                                var xhr = new XMLHttpRequest();
                                                xhr.open("DELETE", "userChangeTariffOptions?contractNumber=" + par1
                                                        + "&tariffOptionId=" + par2 + "&method=unable", false);
                                                xhr.send();
                                                if (xhr.status == 530) {
                                                    alert('Incompatible options')
                                                }
                                            }
                                        }
                                    }</script>

When I moved to spring it stopped working.
What I send through
model.addAttribute();
everything comes. But in this case, it is necessary that ajax be processed and this method is not suitable.
How can you get out of this situation?

On one page, I fixed the problem by adding

resp.addHeader("Access-Control-Allow-Origin", "*");
                resp.addHeader("X-XSS-Protection", "0");
                resp.setStatus(409);


But on one of them, I have such a list of errors. What does it mean?

17:43:49,910 ERROR [io.undertow.request] (default task-49) UT005023: Exception handling request to /WEB-INF/user/userTariffOptions.jsp: org.apache.jasper.JasperException: java.lang.NullPointerException
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:473)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:402)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:346)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
  at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
  at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:81)
  at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
  at io.undertow.jsp.JspFileHandler.handleRequest(JspFileHandler.java:32)
  at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:274)
  at io.undertow.servlet.handlers.ServletInitialHandler.dispatchToPath(ServletInitialHandler.java:209)
  at io.undertow.servlet.spec.RequestDispatcherImpl.forwardImpl(RequestDispatcherImpl.java:221)
  at io.undertow.servlet.spec.RequestDispatcherImpl.forwardImplSetup(RequestDispatcherImpl.java:147)
  at io.undertow.servlet.spec.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:111)
  at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
  at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
  at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1257)
  at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
  at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
  at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
  at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
  at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
  at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:115)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:158)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:121)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:134)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
  at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
  at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
  at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
  at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
  at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
  at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
  at org.apache.jsp.WEB_002dINF.user.userTariffOptions_jsp._jspService(userTariffOptions_jsp.java:133)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
  ... 103 more

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sirs, 2016-10-01
@sirs

In spring mvc, you can return the status in several ways, if the status needs to be returned one and the same all the time, then the @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) annotation is suitable, just hang it on the method in the controller.
In your case, this option is not suitable (if I understand correctly). You need to do this:

public ResponseEntity<?> userNumberOperationsDelete(HttpServletRequest req, HttpServletResponse resp, Locale locale, Model model,
                                             @RequestParam(value = "number") String number,
                                             @RequestParam(value = "status") String status) {
...
 } else {
                return new ResponseEntity<>(errorMessage, HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }
    return new ResponseEntity<>(HttpStatus.OK);
}

I don’t know where you found status 530, but spring doesn’t know it and I strongly advise you not to use custom statuses when working with frameworks. Take the INTERNAL_SERVER_ERROR status.

S
Sergey, 2016-10-01
@void01

maby return ResponseEntity.status(530).body(null);
, in general, you have an HttpServletResponse in your controller, you can also set a status for it, you don’t have to run
this request through MVC ..
use @ResponseBody and respond without any stupid request-response models
like this ..

@RequestMapping(value = "/admin",method = RequestMethod.DELETE)
    public @ResponseBody String responseTest(HttpServletResponse response, HttpServletRequest request
    ) throws ResourceNotFoundException {
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("X-XSS-Protection", "0");
        response.setStatus(530);
        return "";
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question