在管理页面登录后,我尝试使用saveOrUpdate或delete,但是ID未发送到后端。 Delete和saveOrUpdate位于UserController中,而LogIn位于LoginCotroller中,我认为会话存在问题,但是我在客户端服务器应用程序中很菜鸟。我使用System.out.println来帮助我了解后端发生了什么,但是从一开始我就期望ID时返回null。 我在没有登录的另一个项目中使用了这个jsp,没关系。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
</script>
</head>
<body onload="load();">
<% if (session.getAttribute("loggedInUserA").equals(1) ) { %>
<input type="hidden" id="userId">
Name: <input type="text" id="name" required="required" name="name"><br>
Password: <input type="text" id="password" required="required" name="password"><br>
Acces: <input type="text" id="acces" required="required" name="acces"><br>
<button onclick="submit();">Submit</button>
<table id="table" border=1>
<tr> <th> Name </th> <th> Password </th> <th> Acces </th> <th> Edit </th> <th> Delete </th> </tr>
</table>
<script type="text/javascript">
data = "";
submit = function(){
$.ajax({
url:'saveOrUpdate',
type:'POST',
data:{userId:$("#userId").val(),name:$('#name').val(),password:$('#password').val(),acces:$('#acces').val()},
success: function(response){
alert(response.message);
load();
}
});
}
delete_ = function(id){
console.log('id', id);
$.ajax({
url:'delete',
type:'POST',
data:{userId:id},
success: function(response){
alert(response.message);
load();
}
});
edit = function (index){
$("#userId").val(data[index].userId);
$("#name").val(data[index].name);
$("#password").val(data[index].password);
$("#acces").val(data[index].acces);
}
load = function(){
$.ajax({
url:'list',
type:'POST',
success: function(response){
data = response.data;
$('.tr').remove();
for(i=0; i<response.data.length; i++){
console.log(response.data[i].userId);
$("#table").append("<tr class='tr'> <td> "+response.data[i].name+" </td> <td> "+response.data[i].password+" </td> <td> "+response.data[i].acces+" </td> <td> <a href='#' onclick= edit("+i+");> Edit </a> </td> </td> <td> <a href='#' onclick='delete_("+response.data[i].userId+");'> Delete </a> </td> </tr>");
}
}
});
}
</script>
<h2>
<a href="/SpringMVC/">Back!!!</a>
</h2>
<% } else {
String redirectURL = "http://youtube.com";
response.sendRedirect(redirectURL); } %>
UserController
@Controller
@RequestMapping(value="user")
public class UserController {
@Autowired
UserServices userServices;
@RequestMapping(value = "/page", method = RequestMethod.GET)
public ModelAndView getPage() {
ModelAndView view = new ModelAndView("admin");
return view;
}
@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> getSaved(User user) {
Map<String, Object> map = new HashMap<String, Object>();
if (userServices.saveOrUpdate(user)) {
map.put("status", "200");
map.put("message", "Your record have been saved successfully");
}
return map;
}
@RequestMapping(value = "/list", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> getAll(User user) {
Map<String, Object> map = new HashMap<String, Object>();
List<User> list = userServices.list();
if (list != null) {
map.put("status", "200");
map.put("message", "Data found");
map.put("data", list);
} else {
map.put("status", "404");
map.put("message", "Data not found");
}
return map;
}
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> delete(User user) {
Map<String, Object> map = new HashMap<String, Object>();
if (userServices.delete(user)) {
System.out.println("UserController "+user.getUserId());
map.put("status", "200");
map.put("message", "Your record have been deleted successfully");
}
return map;
}
@RequestMapping(value = "/page1", method = RequestMethod.GET)
public ModelAndView getPage1() {
ModelAndView view = new ModelAndView("user1");
return view;
}
}
LoginController
@Controller
@RequestMapping(value="user")
public class LoginController {
@Autowired
UserServices userServices;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String showLoginForm() {
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String verifyLogin(@RequestParam String name,@RequestParam String password,
HttpSession session,Model model) {
User user=userServices.loginUser(name, password);
if (user==null) {
System.out.println("login control user null");
model.addAttribute("loginError","Error logging in. Please try again");
return "login";
}
session.setAttribute("loggedInUser", user);
session.setAttribute("loggedInUserA", user.getAcces());
return "admin";
}
}
UserServiceImplementation
@Service
public class UserServicesImpl implements UserServices {
@Autowired
UserDao userDao;
@Override
public List<User> list() {
// TODO Auto-generated method stub
return userDao.list();
}
@Override
public boolean delete(User user) {
// TODO Auto-generated method stub
System.out.println("UserServices "+user.getUserId()+" "+user.getName() +" "+user.getPassword() + " "+user.getAcces());
return userDao.delete(user);
}
@Override
public boolean saveOrUpdate(User user) {
// TODO Auto-generated method stub
System.out.println("UserServices "+user.getUserId()+" "+user.getName() +" "+user.getPassword() + " "+user.getAcces());
return userDao.saveOrUpdate(user);
}
public User loginUser(String name, String password) {
User user=userDao.findUser(name);
if (user != null && user.getPassword().equals(password)) {
System.out.println("User Login"+user.getUserId()+" "+user.getName());
return user;
}
return null;
}
}
UserDaoImplementation
@Repository
@Transactional
public class UserDaoImpl implements UserDao {
@Autowired
SessionFactory session;
@SuppressWarnings("unchecked")
@Override
public List<User> list() {
// TODO Auto-generated method stub
return session.getCurrentSession().createQuery("from User").list();
}
@Override
public boolean delete(User user) {
// TODO Auto-generated method stub
try {
System.out.println("UserDaoImpl"+user.getUserId());
session.getCurrentSession().delete(user);
}catch(Exception ex) {
return false;
}
return true;
}
@Override
public boolean saveOrUpdate(User user) {
// TODO Auto-generated method stub
session.getCurrentSession().saveOrUpdate(user);
return true;
}
@Override
public User findUser(String name) {
Criteria criteria = session.getCurrentSession().createCriteria(User.class);
User user=(User) criteria.add(Restrictions.eq("name", name)).uniqueResult();
return user;
}
}
答案 0 :(得分:0)
提交Ajax时,请检查id值是否符合您的期望:
submit = function(){
console.log($("#userId").val())
}