Issue
I'm studying the restful API. In this project, I wanna log-in using restful API. So, I made two projects. The one is spring MVC. It just shows the web page. Another is the restful API. And It worked well but when I call the web page, it doesn't work.
I found several solutions, but those not for me.
The one checks import org.springframework.web.servlet.ModelAndView
. I already used it.
Another used String as the return type in the controller instead of ModelAndView.
This is my 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";
}
}
And This is my javascript for going to another page(welcome).
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);
}
And This is my spring web setting 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>
And this is 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>
Actually, I already used <mvc:annotation-driven />
.
How can I solve this problem?
Help me, please.
Additional 1. This is my project explorer.
Answer (modified)
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"localhost:8080/welcome");
$(document.body).append(f);
f.submit();
Solution
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"localhost:8080/welcome");
$(document.body).append(f);
f.submit();
Answered By - Akash