-1

I am in the process of getting to grips with the Spring MVC Framework. I have worked with the PHP Symfony Framework before and there are similar principles. However, I am struggling to create a model with my controller page that will handle a form submission. When a user signs into my system they are either a developer or a project manager. I have one class called a User class, and another class called Role - the latter determines whether the user has the role of Developer or Project Manager. I have the logic however, if you can advise me of how to handle form submission in my method.

I imported the User class. 1) This method displays a form "Get" method

@RequestMapping(value="/user-login", method=RequestMethod.GET)
public ModelAndView loginForm(){
    return new ModelAndView("login-form");
} 

2) This is the method that I am trying to use when retrieving the data "POST" method

@Autowired(value="/user-login", method=RequestMethod.POST)
public ModelAndView loginForm() {
    if(user.getRole()=="User"){
        System.out.println("This is the Developer View");
    } else if(user.getRole()=="Admin"){
        System.out.println("This is the Admin view");
    }
}
kryger
  • 12,906
  • 8
  • 44
  • 65
codespider
  • 51
  • 1
  • 2
  • 7
  • you are not suppose to use `==`, use `.equals()` instead – OPK Dec 28 '15 at 21:08
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – kryger Dec 28 '15 at 21:59

2 Answers2

1

Like i mentioned in the comment, in java when you comparing String, you use .equals(), not ==, also you probably want to call toUpperCase() or toLowerCase() also, because string comparison is case sensitive.

In your post method, if you are trying to bind user input to the java variable, look into @RequestParam

I do not recommend the way you are handling login, use spring security is more of a standard way. http://www.mkyong.com/spring-security/spring-security-custom-login-form-annotation-example/

OPK
  • 4,120
  • 6
  • 36
  • 66
0

It depends on the type of form you're using. If its a spring form you will have to modify your methods and logic. A spring form uses an empty object and populates it with the values submitted through the form. You can then access that object from the method which handles the request.

Please refer to the following tutorial for more on Spring form tags: http://www.baeldung.com/spring-mvc-form-tutorial

If you are using a regular HTML form, then you can pass in the parameter to the method which processes the form request.

public String handleForm(@Requestparam("name") String name){
//handle request
return "nextpage"
BeardedDev
  • 141
  • 8