简体   繁体   English

如何更新按钮上已更改的数据?

[英]How can I update changed data on button?

I want to edit data of the user by inputting new value in the textbox such as his first name and show these changes in his profile. 我想通过在文本框中输入新值(例如他的名字)来编辑用户的数据,并在个人资料中显示这些更改。 I tried to use Ajax but I don't understand what the url should be in there. 我尝试使用Ajax,但我不明白该在其中输入url。 That's my jsp file with edit form and Ajax script: 那是我的带有编辑表单和Ajax脚本的jsp文件:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<script type="text/javascript">
    $("edit").click(function() {
        var firstName = $(request.getParameter("firstname")).val();
        $.ajax({
            url: '',
            type: 'POST',
            data: {
                firstName: firstName
            }
        });
    });
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${title}</title>
</head>
<body>
<h4>To edit your information fill the fields, please.</h4>
    <form name='f' action="${pageContext.request.contextPath}/j_spring_security_check" method='POST'>
      <table>
         <tr>
            <td>First Name:</td>
            <td><input type='text' name='firstname' id='firstname' value='${firstname}'></td>
         </tr>
         <tr>
            <td><input name="edit" type="submit" value="Submit" /></td>
         </tr>      
      </table>
    </form> 

I have update and get methods for the first name in one of the classes: 我有一个类的名字的更新和获取方法:

public class UserInfoDAOImpl extends JdbcDaoSupport implements UserInfoDAO {    
    @Autowired
    public UserInfoDAOImpl(DataSource dataSource) {
        this.setDataSource(dataSource);
    }    
//.....other methods

@Override
public void editFirstName(String userName, String fName){
    String sql = "update Users set FirstName = ? where Username = ?";
    Object[] params = new Object[] {fName, userName};
    this.getJdbcTemplate().update(sql, params);
}

@Override
    public String getFirstName(String userName) {
        // TODO Auto-generated method stub      
        String sql = "select u.FirstName from Users u where u.Username = ? "; 
        Object[] params = new Object[] { userName };         
        String firstName = (String)getJdbcTemplate().queryForObject(sql, params, String.class);
        return firstName;
    }
}

So I don't really get to use these already written methods to update the data and get these changes in user profile. 因此,我实际上并没有使用这些已编写的方法来更新数据并在用户配置文件中进行这些更改。 I am new to Ajax so I'm glad to get any help. 我是Ajax的新手,因此很高兴获得任何帮助。

You are wrong on your JSP, the j_spring_security_check is default URL Spring Login. 您在JSP上输入的错误, j_spring_security_check是Spring URL的默认URL。

For updating firstname you must do @RequestMapping @ResponseBody on your Controller de handle the Ajax Request. 为了更新名字,您必须在处理Ajax请求的Controller上执行@RequestMapping @ResponseBody

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM