0

我有一个要在 CloudFoundry 上运行的基本 Spring Web 应用程序(Spring MVC 项目)。我采用了默认的 HelloWorld 项目并将其添加到其中。我已经安装了 CloudFoundry STS 扩展,创建了一个服务器,将我的应用程序发布到 CF 站点。“主页”页面同时显示在我的本地主机服务器和 CF 服务器上。都好。但是,当我单击唯一的链接将我带回 HomeController 到不同的方法/视图时,我在 CF 服务器上收到“资源不可用”错误,尽管它在我的 localhost(本地 PC)服务器上运行良好。

在我的本地 PC 上:

  1. 网址是:(http://localhost:8080/myapp 正确)
  2. 初始页面 (home.jsp) 显示一个链接:(<a href="/myapp/property">Property</a>正确)
  3. 将鼠标悬停在链接上会在状态栏中显示:( http://localhost:8080/myapp/property 正确)
  4. 单击会将我带到映射到的方法/property并显示属性页 ( property.jsp)。(正确的)

在 CloudFoundry 上:

  1. 网址是:(http://myapp.cloudfoundry.com/正确)
  2. 初始页面 ( home.jsp) 显示与我的本地 PC 上的相同。(正确的)
  3. 将鼠标悬停在链接上会在状态栏中显示:(http://myapp.cloudfoundry.com/myapp/property我认为是正确的)。
  4. 单击获取 ' esource not available
  5. 当我进入位置窗口并myapp从 url 中删除时,它可以工作。

下面是全部代码,不过我想这只是我自己对本地PC和CloudFoundry这两个环境的一些误解。希望有人可以教育我了解我在这里不知道的内容,以使应用程序在本地和 CloudFoundry 两种环境中都能正常工作。

这是 home.jsp 的 HTML,初始页面

<html>
    <head></head>
    <body>
        <a href="/myspp/property">Property</a>
    </body>
</html>

HomeController 是:

package com.myapp.app;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.myapp.services.PropertyServicesImpl;

/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
    private static final String VIEW_HOME = "home";
    private static final String VIEW_PROPERTY = "property";
    private static final String ACQUISITIONS = "acquisitions";
    @Autowired private PropertyServicesImpl propertyServices;   

    /**
* Shows home view
    */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView home(Locale locale, Model model) {
        return new ModelAndView(VIEW_HOME);
    }

    /**
    * Shows Property.jsp with jQuery tabs.
    */
    @RequestMapping(value = "/property", method = RequestMethod.GET)
        public ModelAndView property(Locale locale, Model model) {
            return new ModelAndView(VIEW_PROPERTY);
    }
}
4

2 回答 2

1

而不是在您的视图中放置一个固定值,最好获取请求的上下文路径,然后将其添加到您视图中的路径中。

将以下导入添加到您的 Home 控制器中;

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

然后在 RequestMapping 方法中获取当前请求对象并创建一个 UrlPathHelper 实例并获取请求上下文的基本路径;

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
UrlPathHelper helper = new UrlPathHelper();

String baseURL = helper.getContextPath(request);

因此,当从本地 vFabric 运行时,baseURL 将为“/myapp”,而当从 Cloud Foundry 实例运行时,它将为“”

剩下的就是将它添加到模型中并在视图中使用它;

model.addAttribute("relPath", baseURL);

我用 STS 中的 Spring MVC 模板项目对此进行了测试,它工作得很好,我的 HomeController 看起来像这样;

package com.vmware.mvctest;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.util.UrlPathHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.sun.tools.internal.ws.processor.model.Request;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! the client locale is "+ locale.toString());
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        UrlPathHelper helper = new UrlPathHelper();

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);
        String baseURL = helper.getContextPath(request);

        model.addAttribute("serverTime", formattedDate );
        model.addAttribute("relPath", baseURL);

        return "home";
    }

}

我的观点是这样的;

<%@ page session="false" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
<h1>
    Hello world! (${relPath}) 
</h1>
<a href="${relPath}/home">home</a>
<P>  The time on the server is ${serverTime}. </P>
</body>
</html>
于 2012-06-14T10:22:42.290 回答
1

CF 中的上下文路径是 {app.name}.cloudfoundry.com 而不是 {app.name}.cloudfoundry.com/{app.name}

在您的 jsp 中替换<a href="/myapp/property">Property</a><a href="/property">Property</a>.

于 2012-06-14T10:27:17.030 回答