有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将Spring MVC与AngularJS uirouter结合使用?

如何设置SpringMVC来捕获所有请求并返回我的欢迎页面(index.html)

我的总体意图是使用SpringMVC拦截带有“/api/”的任何内容,以便我可以调用后端,但任何其他url都应该返回“index.html”,以便ui路由器可以尝试显示正确的视图

但是,使用当前设置,我可以使用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";
    }
}

网络。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>

索引。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>


共 (2) 个答案

  1. # 1 楼答案

    你搞错了

    只发送一个服务器页面(index.jsp),Angular是SPA

    之后,Spring应该提供所有RESTful API,以使应用程序具有角度

    ui路由器是客户端站点路由库

    public class HelloWorldController extends AbstractController{
    
        @Override
        protected ModelAndView home(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    
            ModelAndView model = new ModelAndView("home");
    
            return model;
        }
    }
    

    这应该回家。按照您的配置进行操作

  2. # 2 楼答案

    我是如何解决的:

    • index.html移动到/views中,并将其重命名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”前缀的请求,并返回使用ui路由器处理URL的索引页的