<script>
$(function () {
$("#submit").click(function () {
var username = $("#username").val();
var password = $("#password").val();
var user = {username: username, password: password};
var jsonUser = JSON.stringify(user);
$.ajax({
url: "log_in.action",
type: "post",
timeout: 3000,
data: {json: jsonUser},
dataType: "json",
success: function (data) {
alert(data.status);
if (data.status == 1) {
location.href = "log_toIndex.action";
} else {
alert(data.msg);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});
return false;
});
});
</script>
/*
* Copyright (c) 2016.
*/
package cn.njty.manager.action;
import cn.njty.manager.dto.JsonMsg;
import cn.njty.manager.model.User;
import cn.njty.manager.service.LoginService;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by larobyo on 2016/7/29.
* <p></p>
*
* @author larobyo
* @version 1.0
*/
@Controller
public class LogAction extends ActionSupport {
private String username;
private String password;
@Resource
private LoginService loginService;
private String json;
public void in() throws IOException, ServletException {
Gson gson = new Gson();
JsonMsg jsonMsg = new JsonMsg();
/**
* 非空校验
*/
if (username == null || username.trim().equals("") || password == null || password.trim().equals("")) {
jsonMsg.setStatus(0);
jsonMsg.setMsg("用户名或密码为空!");
sendMsg(gson.toJson(jsonMsg));
}
User user = loginService.getUser(username, password);
if (user == null) {
jsonMsg.setStatus(0);
jsonMsg.setMsg("用户名或密码错误!");
sendMsg(gson.toJson(jsonMsg));
} else {
ActionContext.getContext().getSession().put("user", user);
jsonMsg.setStatus(1);
jsonMsg.setMsg("登录成功!");
sendMsg(gson.toJson(jsonMsg));
}
}
public String out() {
User user = (User) ActionContext.getContext().getSession().get("user");
if (user != null) {
ActionContext.getContext().getSession().remove("user");
}
return "success";
}
public String ui() {
return "ui";
}
public String toIndex() {
return "toIndex";
}
public void sendMsg(String content) throws IOException {
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
response.getWriter().write(content);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getJson() {
return json;
}
public void setJson(String json) {
this.json = json;
}
}
学点HTML,js基础,按钮是submit的话,会提交form,那么如果你不return false,就会提交两次。。。
很感谢你,我把form中的submit改成了button,这样看着更清晰一些。js和html还是理解的不够深入。
换成type='button'就没问题了!!!
问题已解决,很感谢
submit会提交表单,给button加点击事件就好了啊,所以会执行两次提交,一次表单提交,一次点击事件ajax提交。至于return false,你试试把它去掉看看,你就懂了
去掉return false ajax中的跳转不会执行
form表单用submit,你的用button就可以了
我把form里的submit改成了button,这样看着更清晰
return false js 不再 向下 执行。。。
理解了,很感谢