javascript - 我的 Controller 返回 ModelAndView 但它不加载 jsp

标签 javascript java spring-mvc jsp

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

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

这是我的 Controller 。

@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>

其实我已经用过<mvc:annotation-driven /> 。 我怎么解决这个问题? 请帮助我。

<小时/>

附加1。 这是我的项目资源管理器。

enter image description here

<小时/>

答案(已修改)

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

最佳答案

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

关于javascript - 我的 Controller 返回 ModelAndView 但它不加载 jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56355229/

相关文章:

Java 构造函数日期设置

java - DataForm 发布值但在服务器上为空

Java返回语句优先级

Javafx TableView在表格自动刷新时仍然选择最后选择的行

spring - Arvixe 网络主机 - Spring MVC 项目找不到网址

java - Spring Security 安全自定义页面

javascript - 在 Javascript 中,直接后跟空花括号的变量是什么意思?

javascript - 在可变数量的集合中寻找没有重复字符的最长公共(public)子序列?

javascript - Mootools - 如何在开头附加文本

javascript - AngularJS 中的自定义验证比较两个日期