0

我正在尝试将 spring mvc 与 jsf 和 jquery 集成。

该页面仅显示硬编码文本,尽管执行了 3 秒计时器并重新渲染了页面,但是随后内存使用量激增,一切都开始崩溃。这是一个示例页面,其中应该每 3 秒生成一个随机数

文件夹结构

-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    

模板.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>

欢迎.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
@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 片段

<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 内容

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

<mvc:annotation-driven />
4

1 回答 1

1

Welcome.xhtml 中的问题是您正在触发 ajax 以获取相同的页面,即 welcome.xhtml

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

更改 url :'welcome.xhtml'url :'welcome'解决您的问题(假设您要将 ajax 发送到rest/welcome

您还在控制器级别和方法级别上放置了相同的请求映射(休息/欢迎)。我不确定您的设计,但您可以从其中一个中删除映射

所以改变

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

//RequestMapping("rest/welcome") note i have commented this
public class HelloWorld
于 2013-08-08T09:15:45.013 回答