java - How to use Spring MVC to update my current user data?

I'm beginner to learn spring MVC, Now I have a problem can't solve. I spent a lot of time want to use Spring MVC to update current user data,but always go round in circles.

For Example login user is [test2] and I want to update mAccount and mPassword,how to get login session and click jsp form to update.What should I do?(mId is Primary Key)

Now when I click submit to update, but SQL database mAccount and mPassword doesn't have any change. I think my controller must have problem to get login session,so I can't update what I want.

Please excuse me for my poor English,Please help me to solve this problem. I really need help!

SQL Member database image Link:

enter image description here

This is my Question Link:(Full Code inside)

enter link description here

Dao:

public MemberBean getMemberBymId(int mId);

DaoImpl:

    @Override
public MemberBean getMemberBymId(int mId) {
    MemberBean mb = null;
    Session session = factory.getCurrentSession();
    String sql = "FROM MemberBean mb WHERE mb.mId=:mid";
    mb = (MemberBean) session.createQuery(sql).setParameter("mid", mId).uniqueResult();
    return mb;
}
@Override
public void updateMember(MemberBean mb) {

    String hql = "UPDATE MemberBean mb SET mb.mAccount =:mAccount , mb.mPassword =:mPassword WHERE mId =:mId";
    Session session = factory.getCurrentSession();

    session.createQuery(hql).setParameter("mAccount", mb.getmAccount()).setParameter("mPassword", mb.getmPassword())
            .setParameter("mId", mb.getmId()).executeUpdate();

}

Service:

MemberBean getMemberBymId(int mId);

void updateMember(MemberBean mb );

Serviceimpl:

@Transactional
@Override
public MemberBean getMemberBymId(int mId) {
    MemberBean mb = null;
    mb = dao.getMemberBymId(mId);
    return mb;
}

@Transactional
@Override
public void updateMember(MemberBean mb) {
    if (mb.getmAccount() != null && mb.getmPassword() != null) {
        dao.updateMember(mb);
    }
}

Controller:

@RequestMapping(value = "/UpdateMemberForm/{mId}", method = RequestMethod.GET)
public String AddLoginMemberBeantoUpdateForm(Model model,HttpSession session,@PathVariable Integer mId) {
    MemberBean mb = memberservice.getMemberBymId(mId);
    model.addAttribute("MemberBean", mb);
    return "register/updateMember";
}

@RequestMapping(value = "/UpdateMemberForm", method = RequestMethod.POST)
public String UpdateMember(@ModelAttribute("MemberBean")MemberBean mb, BindingResult result ) {

    memberservice.updateMember(mb);
    return "index";
}

Jsp:

    <form:form method="POST"  modelAttribute="MemberBean">
        <table>
            <tr>
                <td>帳號:</td>
                <td><form:input id="mAccount"  path="mAccount" type="text" value="${mb.mAccount}"
                     size="10"/>
                </td>
            </tr>
            <tr>
                <td>密碼:</td>
                <td><form:input id="mPassword"  path="mPassword" type="text" value="${mb.mPassword}"
                     size="10"/>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center"><input type="submit" value="更新"  > 
                </td>
            </tr>
        </table>            
    </form:form>

1 Answer

  1. Denny- Reply

    2019-11-16

    Correct me if I am wrong,You want to update a Account,I think problem consist of updateMember function.

       //Let me show a example update a object.
      Customer customerToUpdate = customerRepository.getOne(id);
      customerToUpdate.setName(customerDto.getName);
      customerRepository.save(customerToUpdate);
      //But your code directly updating a user account without find  the user  which is gonna changed.I could't examine full of codes but ı think problem occurs here.
          memberservice.updateMember(mb);
    

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>