【问题标题】:My controller returns ModelAndView but it doesn't load jsp我的控制器返回 ModelAndView 但它不加载 jsp
【发布时间】:2026-02-07 02:05:02
【问题描述】:

我正在研究 restful API。在这个项目中,我想使用 RESTful API 登录。所以,我做了两个项目。一个是spring MVC。它只显示网页。另一个是restful API。它运行良好,但是当我调用网页时,它不起作用。

我找到了几个解决方案,但不适合我。 一个检查import org.springframework.web.servlet.ModelAndView。我已经用过了。 另一个使用 String 作为控制器中的返回类型,而不是 ModelAndView。

这是我的控制器。

@Controller
public class LoginController {
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView displayLogin(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView model = new ModelAndView("login");
        return model;
    }

    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public String displayWelcom(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView model = new ModelAndView("welcome");
        System.out.println("Welcome model");
        return "welcome";
    }
}

这是我用于转到另一个页面的 javascript(欢迎)。

function validateForm() {
        var xmlhttp = new XMLHttpRequest();
        var url = "http://localhost:8080/users/www.t3q.com";
        //      + window.location.hostname;
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == XMLHttpRequest.DONE && xmlhttp.status == 200) {
                alert('1');
                var json = JSON.parse(xmlhttp.responseText);
//              if(json.isSuccess == 'true')
//              {
                    var xmlhttp2 = new XMLHttpRequest();
                    xmlhttp2.open("GET", "http://localhost:8081/SpringMVCloginExample/welcome", true);
                    xmlhttp2.send();
//              } else {
//                  alert(json.reason);
//              }
                console.log(xmlhttp.responseText);
            }
        };

        xmlhttp.open("POST", url, true);

        xmlhttp.setRequestHeader("Content-Type",
                "application/json;charset=UTF-8");

        var userData = JSON.stringify($("#loginForm").serializeObject());

        console.log(userData);
        xmlhttp.send(userData);
    }

这是我的 Spring Web 设置 XML。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

<!--    <mvc:annotation-driven /> -->

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

</beans>

这是 web.xml。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>SpringMVCloginExample</display-name>


    <servlet>
        <servlet-name>springLoginApplication</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath://resource//springWeb.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springLoginApplication</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

其实我已经用过&lt;mvc:annotation-driven /&gt;了。 我怎么解决这个问题? 请帮帮我。


附加 1。 这是我的项目浏览器。


答案(修改)

var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"localhost:8080/welcome");
$(document.body).append(f);
f.submit();

【问题讨论】:

    标签: javascript java spring-mvc jsp


    【解决方案1】:
    var f = document.createElement("form");
    f.setAttribute('method',"post");
    f.setAttribute('action',"localhost:8080/welcome");
    $(document.body).append(f);
    f.submit();
    

    【讨论】:

    • 我在帖子中添加了我的项目浏览器。当我在浏览器中使用 URL 访问网页时效果很好。
    • 您是否尝试通过 ajax 调用打开页面?在那种情况下,它不会发生。这有点像 ajax 调用等待来自服务器的响应(这与提交表单不同)。
    • 啊...谢谢。我明白。那么,要进入欢迎页面,我该怎么做呢?
    • 您可以在您的JS代码中创建一个表单,通过JS提交该表单,然后控制器将打开您的JSP。像这样的东西 var f = document.createElement("form"); f.setAttribute('method',"post"); f.setAttribute('action',"localhost:8080/welcome"); f.submit()
    • 哇!非常感谢。当我使用它时,它工作得很好!但我添加了如下代码: var f = document.createElement("form"); f.setAttribute('method',"post"); f.setAttribute('action',"localhost:8080/welcome"); $(document.body).append(f); f.submit();
    最近更新 更多