mangusbrother
mangusbrother

Reputation: 4156

spring MVC + jsf + jquery

I am trying to integrate spring mvc with jsf and jquery.

The page shows only the hard-coded text, although the 3s timer is executed and the page is reRendered, however then the memory usage spikes up and everything starts to crash. This is a sample page where a random number is supposed to be generated every 3 seconds

Folder Structure

-Web Content
   - index.xhtml
   - template.xhtml
   - rest
      - welcome.xhtml
   -WEB-INF
      - web.xml
      - testServlet-servlet.xml
      - faces-config.xml
      - lib
         - *all libraries*
-Java Resources 
   -src
      - com
         -controller
            -HelloWorld.java    

Template.xhtml

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
    <title><ui:insert name="title">Default Title</ui:insert></title>
    <link href='http://fonts.googleapis.com/css?family=Droid+Sans:400,700' 
        rel='stylesheet' type='text/css' />

</h:head>

<h:body>
    <div >
        <ui:insert name="content" />
    </div>
 </h:body>

Welcome.xhtml

<ui:composition template="../template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j">

<ui:define name="title">
    Sample Page
    </ui:define>


<ui:define name="content">
    <script type="text/javascript"
        src="http://code.jquery.com/jquery-1.10.1.min.js" />
    <script type="text/javascript">
        function ajaxFunction() {
            $.ajax({
                url : 'welcome.xhtml',
                success : function(data) {
                    $('#result').html(data);
                }
            });
        }
    </script>
    <script type="text/javascript">
        var intervalId = 0;
        intervalId = setInterval(ajaxFunction, 3000);
    </script>

    ${message} 
    SUCCESS:
    <p id="result"></p>
</ui:define>

Controller

@Controller
@RequestMapping("rest/welcome")
public class HelloWorld
{
    @RequestMapping("rest/welcome")
public ModelAndView ajaxTest()
    {
        return new ModelAndView("rest/welcome", "message",
                "This is a test message");
    }

    @RequestMapping(value = "rest/welcome", method = RequestMethod.GET)
    public @ResponseBody
    String getTime()
    {
        Random rand = new Random();
        float r = rand.nextFloat() * 100;
        String result = "<br>Next Random # is <b>" + r
                + "</b>. Generated on <b>" + new Date().toString() + "</b>";
        System.out.println("Debug Message from Controller.."
                + new Date().toString());
        return result;
    }
}

Web.xml snippet

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/testServlet-servlet.xml</param-value>
</context-param>

testSevlet-servlet.xml content

     <context:component-scan
    base-package="com.controller" />

<mvc:annotation-driven />

Upvotes: 0

Views: 452

Answers (1)

harrybvp
harrybvp

Reputation: 2505

Problem in welcome.xhtml is that you are firing ajax to get same page i.e. welcome.xhtml

$.ajax({
                url : 'welcome.xhtml',
                success : function(data) {
                    $('#result').html(data);
                }
            });

change url :'welcome.xhtml' to url :'welcome' to solve your problem (assuming you want to send ajax to rest/welcome )

Also you have put same requestmapping (rest/welcome) on Controller level as well as method level.I am not sure of your design but you can remove mapping from one of them

So change

@RequestMapping("rest/welcome")
public class HelloWorld

to

//RequestMapping("rest/welcome") note i have commented this
public class HelloWorld

Upvotes: 1

Related Questions