提交 f878049b 编写于 作者: 爱吃血肠's avatar 爱吃血肠

用户登录使用异步提交方式 解决ajax返回数据乱码问题

上级 85b4c319
......@@ -8,7 +8,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
......@@ -25,10 +25,11 @@ public class UserController {
* 用户登录
* @return
*/
@RequestMapping(value = "/userLogin")
@ResponseBody
@RequestMapping(value = "/userLogin",produces="text/html;charset=UTF-8")
public String userLogin(@ModelAttribute User user) {
LOG.info("表单提交的用户信息:" + user.toString());
return "/user/userLogin";
return "登录成功";
}
/**
......
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.css" rel="stylesheet">
<script src="http://apps.bdimg.com/libs/html5shiv/3.7/html5shiv.min.js"></script>
<script src="http://apps.bdimg.com/libs/respond.js/1.4.2/respond.js"></script>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script>
\ No newline at end of file
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
request.setCharacterEncoding("utf-8");
String path = request.getContextPath();
%>
\ No newline at end of file
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@include file="common/tag.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>商品列表</title>
<%@include file="common/head.jsp"%>
</head>
<body>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading text-center">
<h2>商品列表</h2>
</div>
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<td>商品ID</td>
<td>商品名称</td>
<td>商品价格</td>
<td>商品状态</td>
<td>商品数量</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<c:forEach var="goods" items="${goodslist}">
<tr>
<td>${goods.goodsId}</td>
<td>${goods.title}</td>
<td>${goods.price}</td>
<td><c:if test="${goods.state==0}">已下架</c:if> <c:if test="${goods.state==1}">销售中</c:if></td>
<td>${goods.number}</td>
<td><button class="btn btn-info" id="goodsBuy" onclick="handler.goodsBuy(${goods.goodsId});">购买</button></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
<%--登录弹出层 输入电话--%>
<div id="loginModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title text-center">
<span class="glyphicon glyphicon-phone"> </span>用户电话
</h3>
</div>
<div class="modal-body">
<div class="row">
<div class="col-xs-8 col-xs-offset-2">
<input type="text" name="userPhone" id="userPhone" placeholder="填写手机号" class="form-control">
</div>
</div>
</div>
<div class="modal-footer">
<%--验证信息--%>
<span id="userPhoneMessage" class="glyphicon"> </span>
<button type="button" id="loginBtn" class="btn btn-success">
<span class="glyphicon glyphicon-phone"></span>提交
</button>
</div>
</div>
</div>
</div>
</body>
<script src="http://apps.bdimg.com/libs/jquery.cookie/1.4.1/jquery.cookie.js"></script>
<script src="<%=path%>/resource/script/handler.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
//初始化业务逻辑script
handler.goods.init({});
})
</script>
</html>
\ No newline at end of file
......@@ -40,7 +40,7 @@
<div class="header">
<h3>用户登录</h3>
</div>
<form id="userLogin" action="/user/userLogin" method="post">
<form id="userLogin" action="" method="post">
<div class="detail">
<ul>
<li>
......@@ -83,9 +83,21 @@
</footer>
</body>
<script>
//用户登录
//用户登录异步提交表单
$('#loginBtn').bind("click", function(){
$('#userLogin').submit();
//$('#userLogin').submit();
var uploadFormData = new FormData($('#userLogin')[0]);
$.ajax({
type: 'POST',
data: uploadFormData,
url: '/user/userLogin',
processData: false,
contentType: false,
async: false,
success: function (data) {
console.log(data);
}
});
});
</script>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@include file="common/tag.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>用户列表</title>
<%@include file="common/head.jsp"%>
</head>
<body>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading text-center">
<h2>用户列表</h2>
</div>
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<td>用户ID</td>
<td>用户名</td>
<td>手机号</td>
<td>积分</td>
</tr>
</thead>
<tbody>
<c:forEach var="user" items="${userlist}">
<tr>
<td>${user.userId}</td>
<td>${user.userName}</td>
<td>${user.userPhone}</td>
<td>${user.score}</td>
<td><fmt:formatDate value="${user.createTime}" pattern="yyyy-MM-dd HH:mm:ss" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册