2

I want to use normal Html tags with Spring MVC instead of spring Html tags but I am unable to get the input value to Controller using simple html tags.

Below is my Code:-

Controller :

@Controller
public class LoginService {

    @RequestMapping(value = "/authenticate", method = RequestMethod.GET)
    public String authenticateUser(Model model,@ModelAttribute("userName") String userName, 
            @ModelAttribute("password") String password) {
        System.out.println("coming in controller    " +userName +" : "+ password);  
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "success";
    }

JSP :

<body>
<h2>${message}</h2>
<form action="authenticate.htm">
    <table>
        <caption>
            <h3>Enter Your Login Credentials</h3>
        </caption>
        <tr>
            <td>User Name</td>
            <td><input type="text" id="userName"></input></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" id="password"></input></td>
        </tr>
        <tr rowspan="2">
            <td><input type="submit"></input></td>
        </tr>
    </table>
</form>

web.xml

   <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>*.htm</url-pattern>
   </servlet-mapping>

1 Answer 1

3

Use RequestParam instead of ModelAttribute.

@RequestMapping(value = "/authenticate", method = RequestMethod.GET)
    public String authenticateUser(@RequestParam("userName") String userName, @RequestParam("password") String password ,Model model) {
        System.out.println("coming in controller    " +userName +" : "+ password);  
        model.addAttribute("message", "Hello Spring MVC Framework!");
        return "success";
    }

you have to change the action url of the form in jsp:

<form action="/authenticate">

update your web.xml with the following line:

<url-pattern>/url-pattern>
Sign up to request clarification or add additional context in comments.

8 Comments

I am Getting Http 404 "HTTP Status 400 - Required String parameter 'userName' is not present" and description is " The request sent by the client was syntactically incorrect (Required String parameter 'userName' is not present). "
clear cache of your browser and try with updated code
I cleared cache and runned the project again without changing form action ..but it was showing same exception
If I modified the form action them current servlet mapping will not work ...right?
Please set "name" to all input type text and use it. Example: <input type="email" id="email" name="email" class="form-control" placeholder="Email address" required="" autofocus=""> <input type="password" id="pass" name="pass" class="form-control" placeholder="Password" required="">
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.