Announcement Announcement Module
Collapse
No announcement yet.
password change form and controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • password change form and controller

    Hi,

    I would like to create a form permitting users to change their password.

    With the @RequestMapping(value = "/change-pass*", method = RequestMethod.POST), i get this error :
    Request method 'GET' not supported

    With the @RequestMapping(value = "/change-pass*", method = RequestMethod.GET), i get another :
    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'password' available as request attribute.

    Thanks for help,


    Here is my jsp
    <table border="0" cellspacing="0" cellpadding="0" align="left" width="400">
    <tr>
    <td><label class="desc" ><fmt:message key="user.password" /></label>
    <form:errors path="password" cssClass="fieldError" />
    <formassword path="password" id="password"
    cssClass="text medium" cssErrorClass="text medium error" showPassword="true" /></td>
    </tr>
    <tr>
    <td><label class="desc" ><fmt:message key="user.confirmPassword" /></label>
    <form:errors path="confirmpassword" cssClass="fieldError" />
    <formassword path="confirmpassword" id="confirmpassword"
    cssClass="text medium" cssErrorClass="text medium error" showPassword="true" /></td>
    </tr>
    </table>


    and my change-pass controller

    @Controller
    @RequestMapping(value = "/change-pass*", method = RequestMethod.GET)
    public class ChangePassController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
    HttpServletResponse response) throws Exception {

    ModelAndView model = new ModelAndView("change-pass");

    User current_user = ( User ) (( SecurityContext ) SecurityContextHolder
    .getContext()).getAuthentication().getPrincipal();

    model.getModel().get("password");
    model.getModel().get("confirmpassword");

    //update the database with the new password

    return model;
    }
    }

  • #2
    Please use [ code][/code ] tags when posting code, that way we can actually read your code ...

    Next don't extend abstractcontroller and use @Controller use either one not both. Also make your form a POST (the initial error indicates that you are only doing a GET!). Also your controller is pretty useless as you expect your newly created model to already contain some values?! In short I really don't get your controller.
    Last edited by Marten Deinum; Jul 9th, 2012, 06:08 AM.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      Please use [ code][/code ] tags when posting code, that way we can actually read your code ...

      Next don't extend abstractcontroller and use @Controller use either one not both. Also make your form a POST (the initial error indicates that you are only doing a GET!). Also your controller is pretty useless as you expect your newly created model to already contain some values?! In short I really don't get your controller.
      Thanks for advice but i didn't get where to put <code></code> tags.

      i added the <form:form commandName="change-pass" method="post" action="change-pas"> to the jsp
      and got the same error

      then i removed the @Controller from the controller class and now got the 404 error

      Comment


      • #4
        Maybe i could ask another way...here is my code to display informations of the user's account. Now it works well but i need to update changed data on user's click on the button register. What i have to do ?
        Is the AbstractController the good class to extend or i better use SimpleFormController ?

        Thks

        <table border="0" cellspacing="0" cellpadding="0" align="left" width="400">
        <tr>
        <td><label class="desc" ><fmt:message key="user.username" /></label><h6>${username}</h6></td>
        <td><label class="desc" ><fmt:message key="user.lastName" /></label>
        <input name="lastname" value=${lastname} ></td>
        <td><label class="desc" ><fmt:message key="user.firstName" /></label>
        <input name="firstname" value=${firstname} ></td>
        </tr>

        <tr>
        <td><label class="desc" ><fmt:message key="user.address.address"/></label>
        <input name="adresse" value=${adresse} ></td>
        </tr>

        <td><label class="desc" ><fmt:message key="user.email"/></label>
        <input name=email value=${email} ></td>
        <td><label class="desc" ><fmt:message key="user.phoneNumber"/></label>
        <input name=phonenumber value=${phonenumber} ></td>
        </tr>

        </table>

        <input type="submit" class="button" name="save" onclick="bCancel=false" value="<fmt:message key="button.save"/>" /></td>


        @Controller
        @RequestMapping( "/user-acc*")
        public class UserCompteController extends AbstractController {

        @Override
        protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("user-acc");

        User current_user = ( User ) (( SecurityContext ) SecurityContextHolder
        .getContext()).getAuthentication().getPrincipal();
        model.addObject("iduser", current_user.getId());
        model.addObject("username", current_user.getUsername());
        model.addObject("fullname",current_user.getFullNam e());
        model.addObject("firstname", current_user.getFirstName());
        model.addObject("lastname", current_user.getLastName());
        model.addObject("adresse", current_user.getAddress().getAddress());
        model.addObject("email", current_user.getEmail());
        model.addObject("phonenumber",current_user.getPhon eNumber());
        return model;
        }

        Comment


        • #5
          if it's what you look for ..

          user update with request attributes on Submit does not work

          Comment


          • #6
            Maybe i could ask another way...here is my code to display informations of the user's account. Now it works well but i need to update changed data on user's click on the button register. What i have to do ?
            Is the AbstractController the good class to extend or i better use SimpleFormController ?
            Have you actually read my post?! Don't mix Controller and @Controller which is what you are currently doing, as mentioned before don't do that..

            I strongly suggest you read the reference guide on how to write form based @Controllers.

            Comment


            • #7
              Originally posted by Marten Deinum View Post
              Have you actually read my post?! Don't mix Controller and @Controller which is what you are currently doing, as mentioned before don't do that..

              I strongly suggest you read the reference guide on how to write form based @Controllers.
              Hello,

              yes i read your post and removed the @controller then got another error.
              Now it works well with the @controller and extend AbstractController at the same time.
              i can add data to the model and display them.
              I just don't know what to do to get changed data back from the model to update the database.
              I have read many documents about how to use spring mvc but still don't get it.

              thks for your help

              Comment


              • #8
                Sigh.. You haven't read my post, well maybe you read it you don't understand it... You really lack the basics of Spring (MVC).

                1) Don't mix @Controller and Controller
                2) Use a form backing object not strings

                Code:
                @Controller
                @RequestMapping( "/user-acc*")
                public class UserCompteController { // 1 Don't extend AbstractController !!!!
                
                  //2 Use a form object
                  @ModelAttribute
                  public User user() {
                    return ( User ) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                  }
                
                  // Initial get for rendering the page
                  @RequestMapping(method=RequestMetod.GET)
                  public String index() {
                    return "user-acc";
                  }
                
                  // Processing the form submit
                  @RequestMapping(method=RequestMethod.POST)
                  public String submit(@ModelAttribute("user") User user, BindResult result) {
                    if (!result.hasErrors() ) {
                      userService.update(user);
                    }
                    return "user-acc";
                  }
                }
                Code:
                <form:form method="post" modelAttribute="user">
                <table border="0" cellspacing="0" cellpadding="0" align="left" width="400">
                <tr>
                <td><label class="desc" ><fmt:message key="user.username" /></label><h6>${user.username}</h6></td>
                <td><label class="desc" ><fmt:message key="user.lastName" /></label><form:input path="lastname" /></td>
                <td><label class="desc" ><fmt:message key="user.firstName" /></label><form:input path="firstname" /></td>
                </tr>
                
                <tr>
                <td><label class="desc" ><fmt:message key="user.address.address"/></label><form:input path="adresse" /></td>
                </tr>
                
                <td><label class="desc" ><fmt:message key="user.email"/></label><form:input path=email type="email" /></td>
                <td><label class="desc" ><fmt:message key="user.phoneNumber"/></label><form:input path=phonenumber type="tel"/></td>
                </tr>
                
                </table>
                <form:form>
                This is all quite clearly explained in the reference guide. You also might want to consider this book (shameless plug that is )

                Comment


                • #9
                  Originally posted by Marten Deinum View Post
                  Sigh.. You haven't read my post, well maybe you read it you don't understand it... You really lack the basics of Spring (MVC).

                  1) Don't mix @Controller and Controller
                  2) Use a form backing object not strings

                  Code:
                  @Controller
                  @RequestMapping( "/user-acc*")
                  public class UserCompteController { // 1 Don't extend AbstractController !!!!
                  
                    //2 Use a form object
                    @ModelAttribute
                    public User user() {
                      return ( User ) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                    }
                  
                    // Initial get for rendering the page
                    @RequestMapping(method=RequestMetod.GET)
                    public String index() {
                      return "user-acc";
                    }
                  
                    // Processing the form submit
                    @RequestMapping(method=RequestMethod.POST)
                    public String submit(@ModelAttribute("user") User user, BindResult result) {
                      if (!result.hasErrors() ) {
                        userService.update(user);
                      }
                      return "user-acc";
                    }
                  }
                  Code:
                  <form:form method="post" modelAttribute="user">
                  <table border="0" cellspacing="0" cellpadding="0" align="left" width="400">
                  <tr>
                  <td><label class="desc" ><fmt:message key="user.username" /></label><h6>${user.username}</h6></td>
                  <td><label class="desc" ><fmt:message key="user.lastName" /></label><form:input path="lastname" /></td>
                  <td><label class="desc" ><fmt:message key="user.firstName" /></label><form:input path="firstname" /></td>
                  </tr>
                  
                  <tr>
                  <td><label class="desc" ><fmt:message key="user.address.address"/></label><form:input path="adresse" /></td>
                  </tr>
                  
                  <td><label class="desc" ><fmt:message key="user.email"/></label><form:input path=email type="email" /></td>
                  <td><label class="desc" ><fmt:message key="user.phoneNumber"/></label><form:input path=phonenumber type="tel"/></td>
                  </tr>
                  
                  </table>
                  <form:form>
                  This is all quite clearly explained in the reference guide. You also might want to consider this book (shameless plug that is )

                  Sure, i have never worked with Spring framework and i'm not accustomed to deal with anotations! I'll try it then give you feedback.
                  Thks a lot

                  Comment


                  • #10
                    Hello,
                    i tried your code and that's what i got (for the lastname field for example) :

                    org.springframework.beans.NotReadablePropertyExcep tion: Invalid property 'lastname' of bean class [com.altran.emi.business.user.User]: Bean property 'lastname' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
                    at org.springframework.beans.BeanWrapperImpl.getPrope rtyValue(BeanWrapperImpl.java:707)


                    Here is the User Entity

                    @Entity
                    @Table(name = "user")
                    @Searchable
                    public class User extends BaseObject implements Serializable, UserDetails {
                    ...
                    private String lastName;
                    ...

                    @Column(name = "first_name", nullable = true, length = 50)
                    @SearchableProperty
                    public String getFirstName() {
                    return firstName;
                    }

                    @Column(name = "last_name", nullable = true, length = 50)
                    @SearchableProperty
                    public String getLastName() {
                    return lastName;
                    }
                    ...
                    }

                    Do you have any idea ?

                    Thks

                    Comment


                    • #11
                      I'm starting to get the feeling that I'm having to do your job and you are just copy/pasting what is written here. If it doesn't work you are copy pasting the errors back again, sit back and wait for a solution.

                      I strongly suggest you read the reference guide, take a look at the sample application(s) to get an understanding on how it works.

                      Also when posting code use [ code][/code ] tags that way it remains readable!!!

                      But here is the answer in short.

                      lastname = lastName
                      firstname = firstName

                      I suggest a read of the JavaBeans specification and how properties work.

                      Comment


                      • #12
                        Hello,

                        I tried your code and i got this error:
                        org.springframework.beans.NotReadablePropertyExcep tion: Invalid property 'lastname' of bean class [com.altran.emi.business.user.User]: Bean property 'lastname' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
                        at org.springframework.beans.BeanWrapperImpl.getPrope rtyValue(BeanWrapperImpl.java:707)

                        Here is the user Entity

                        @Entity
                        @Table(name = "user")
                        @Searchable
                        public class User extends BaseObject implements Serializable, UserDetails {
                        ...
                        private String lastName;
                        ...
                        @Column(name = "first_name", nullable = true, length = 50)
                        @SearchableProperty
                        public String getFirstName() {
                        return firstName;
                        }

                        @Column(name = "last_name", nullable = true, length = 50)
                        @SearchableProperty
                        public String getLastName() {
                        return lastName;
                        }
                        ...
                        }

                        Comment


                        • #13
                          I'm not just copying and pasting your code because it has so many compilation errors!!!
                          I have changed it and reading the reference guide.

                          if you are not pleased to help, don't!
                          nobody forces you do it.

                          do what you want to but BE POLITE!! your solution does not work and it is not a last or first name probleme!

                          Comment


                          • #14
                            The code I post is to give you an idea but from what you postback I get the impression you are just copy/pasting without understanding. Your last post was at least due to this because you did an almost one on one copy at least for the names.

                            Your property is named lastName and not lastname and as such the error is simply in your form tag.

                            I only ask that before you post here you did at least some investigation yourself and try to understand how things work.

                            Comment

                            Working...
                            X