1
我有一個登錄表單(在index.jsp),它是由jQuery的POST函數提交給我的控制器。點擊提交按鈕後,整個視圖將加載到我的錯誤消息元素中 - 而不是輸入到表單輸入中的用戶名。控制器響應整個視圖的AJAX請求
任何想法我錯了嗎?任何幫助將非常感激。
登錄形式
<form class="form-inline" id="loginForm" role="form" style="margin-top: 20px">
<div class="form-group">
<input type="text" name="username" class="form-control" id="username" placeholder="Enter your username"
autocomplete="off" autocapitalize="off">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" id="password" placeholder="Enter your password"
autocomplete="off" autocapitalize="off">
</div>
<div class="form-group">
<button type="submit" name="loginBtn" class="btn btn-default" id="loginBtn">Log in</button>
</div>
<label class="formErrorMsg" id="loginErrorMsg" for="loginBtn"></label>
</form>
login.js
$(document).ready(function(){
// Check that the login credentials entered are valid
$("#loginBtn").click(function(){
event.preventDefault();
$.post("/login", {username : $("#username").val()}, function(result){
$("#loginErrorMsg").html(result)
});
});
});
indexController的
@Controller
public class IndexController {
// Map requests for "/" to index.jsp
@RequestMapping(value = "/")
public ModelAndView returnIndexPage() {
ModelAndView model = new ModelAndView("index");
return model;
}
// Map AJAX request, via login form, to login
@RequestMapping(value = "/login", method = RequestMethod.POST)
@ResponseBody
public String checkLoginCredentials(@RequestParam("username") String username){
return username;
}
}
彈簧調度-servlet.xml中
<mvc:annotation-driven/>
<mvc:resources mapping="/webjars/**" location="/webjars/"/>
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
的web.xml
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>