2

如何設置Spring MVC來捕獲所有請求並返回我的歡迎頁面(index.html)?如何在AngularJS UI路由器上使用Spring MVC?

我的總體目標是使用Spring MVC攔截任何帶有「/ api /」的內容,以便我可以調用後端,但任何其他url都應該返回「index.html」,以便ui-router可以嘗試顯示正確的視圖。

但是我目前的設置,我可以在工地附近使用UI路由器導航,但是當我試圖瀏覽到應當由UI路由器解析的URL我得到這個錯誤:

Problem accessing /app/hi/hello. Reason: 
Circular view path [index.html]: would dispatch back to the current handler URL 
[/app/hi/index.html] again. Check your ViewResolver setup! (Hint: This may be the result 
of an unspecified view, due to default view name generation.) 

我的設置:

BaseController.java

@Controller 
public class BaseController { 

    @RequestMapping("/app/**") 
    public String get() { 
     return "index.html"; 
    } 
} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
    </welcome-file-list> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

MVC-調度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <context:component-scan base-package="com.football_preds"/> 
    <mvc:annotation-driven/> 
    <mvc:resources mapping="/app/**" location="/" /> 

    <bean id="viewResolver" 
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/views/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 
</beans> 

的index.html

<head> 
    <link href="style.css" rel="stylesheet"/> 
    <meta charset="utf-8"> 
    <script src="dist/football-preds.js"></script> 
    <title>Football Predictions</title> 
</head> 

<body ng-controller="MainController"> 
    <section ui-view></section> 

    <button ui-sref="login">Login page</button> 
</body> 

回答

0

我如何解決它:

  • 感動index.html成/意見,並更名爲index.jsp

  • 我的控制器類:

    @Controller 
    public class BaseController { 
    
        // Map all urls to this method aside from ones starting with api 
        @RequestMapping(value = "/**") 
        public String getIndex() { 
         // return the view called "index.html" (in the current directory) 
         return "index"; 
        } 
    
        @RequestMapping(value = "/api/**") 
        @ResponseBody 
        public String testApi() { 
         return "test string"; 
        } 
    } 
    

此,一些如何,設法處理所有請求不加前綴「/ api」的sts並返回使用ui-router處理url的索引頁。

-1

你搞錯了。

僅派發一個服務器頁面(index.jsp),Angular是SPA。

之後,Spring的所有RESTful API都提供了Angulared Application。

ui-router是客戶端站點路由庫。

public class HelloWorldController extends AbstractController{ 

    @Override 
    protected ModelAndView home(HttpServletRequest request, 
     HttpServletResponse response) throws Exception { 

     ModelAndView model = new ModelAndView("home"); 

     return model; 
    } 
} 

這應該返回home.do或按照您的配置。

+0

但是,如何設置GAE/Spring來返回index.jsp而不管網址? – james

+0

編輯,以擁有你想要的。 –

+0

你可以參考這個tut http://www.mkyong.com/spring-mvc/spring-mvc-hello-world-example/也有一個服務器頁面使用。 –