Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3607666
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:24:16+00:00 2026-05-18T21:24:16+00:00

While taking lessons in spring3 I coded a sample from a tutorial. I created

  • 0

While taking lessons in spring3 I coded a sample from a tutorial. I created a controller as below

package my.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import my.spring.form.Contact;

@Controller
public class ContactController {
 @RequestMapping(value ="/addContact",method =RequestMethod.POST)
 public String addContact(@ModelAttribute("contact") Contact ct){
  System.out.println("First Name:" + contact.getFirstname() + "Last Name:" + contact.getLastname());
  return "redirect:contacts.htm";  
 }
 @RequestMapping("/contacts")
 public ModelAndView showContacts() {
  System.out.println("showing contacts");  
  return new ModelAndView("contact", "userEntries", new Contact());
 }
}

Then I decided to play around with it and modified the @ModelAttribute in method parameter from

public String addContact(@ModelAttribute("contact") Contact ct)

to

public String addContact(@ModelAttribute("somevalue") Contact ct)

Still I couldn’t find any change in behaviour of the application. This was a bit of surprise for me. As I understood, the data from form is collected in a Contact object and using @ModelAttribute that object is bound to the parameter ct. This parameter is then used to process inside the method.
Is it that the actual string used inside @ModelAttribute( ) doesn’t matter?

Here is the WEB-INF/jsp/contact.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Contact Manager</title>
</head>
<body>
<h2>Contact Manager</h2>
<form:form action="addContact.htm" commandName="userEntries">
<table>
<tr>
 <td>
 <form:label path="firstname">First Name</form:label> 
 </td>
 <td>
 <form:input path="firstname"/>
 </td>
</tr>

<tr>
 <td>
 <form:label path="lastname">Last Name</form:label> 
 </td>
 <td>
 <form:input path="lastname"/>
 </td>
</tr>

<tr>
 <td colspan="2">
 <input type="submit" value="Add Contact"/>
 </td>
</tr>
</table>

</form:form>
</body>
</html>

and the spring servlet configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation=
 "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 <mvc:annotation-driven/>
 <context:component-scan base-package="my.spring.controller" />
<bean id="viewResolver"
  class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>
</beans>

Finally, the index.jsp forwards to contacts

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>my index</title>
</head>
<body>
<jsp:forward page="contacts.htm"></jsp:forward>

</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:24:16+00:00Added an answer on May 18, 2026 at 9:24 pm

    It will be used for mapping the model attribute to the form:

    <form:form modelAttribute="myObject">
    
    </form>
    

    You will have to use

    public String controllerMethod(@ModelAttribute("myObject") Object obj){
        ....
    }
    

    If you don’t use the same value in both model attribute values you won’t be able to see the error messages of the validation (when doing in the controller result.rejectValue and in the jsp <form:errors/> tag).

    There are no differences to populate the values from the form in the object because you can use html tags instead of spring tags, and in the html tags you only have the name attribute to map the value.

    In case you use @ModelAttribute in a method like below, you will insert in the model an attribute named “myObject” with value the object returned. This is another feauture of this annotation and it will be invoked before any method of the controller.

    @ModelAttribute("myObject") Object obj
    public Object method(){
        ...
        return obj;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to iphone app development, while taking picture from camera I am
Is there a way to exclude fields from different tables while taking DB dump.
I want to give scale in and scale out option while taking picture from
Recently I have modified my code to While taking input form STDIN, I moved
Is it possible to show a preference with the unit (while still only taking
While trying to profile my Perl program, I find that Math::Complex is taking up
I have a handful of sites I am taking down for a while due
While taking a look at this awesome thread I noticed that some examples use
I want to do a foreach loop while taking out members of that foreach
While taking backup of a schema, will it also copies the permissions granted on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.