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

  • SEARCH
  • Home
  • 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 8430177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:32:15+00:00 2026-06-10T05:32:15+00:00

here is the jsp: <c:if test=${!empty USERS}> <form:form method=post action=requestForFriends.html commandName=user> <form:select path=userName> <c:forEach

  • 0

here is the jsp:

<c:if test="${!empty USERS}">
    <form:form method="post" action="requestForFriends.html" commandName="user">
        <form:select path="userName">
            <c:forEach items="${USERS}" var="user">
                <form:option value="${user.userName}"></form:option>
            </c:forEach>
        </form:select>

        <input type="submit" value="Send freindship request" />
    </form:form>
</c:if>

here is the controller’s relevant part:

@RequestMapping("/toAddFriend")
public ModelAndView toAddNewFriend() {
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("USERS", userService.getUsers());
    ModelAndView ret=new ModelAndView("addFriend", model);
    ret.addObject("user", new User());

    return ret;
}

if I do the code above then a brand new User object will be created when I submit the form. But when I click “submit” I would like to get one of the ALREADY EXISTING User instance, which are clearly present in here:

<c:forEach items="${USERS}" var="user">
    <form:option value="${user.userName}"></form:option>

How can I modify my code to get the existing object?
Actually I know an ugly way to get the existing instance. By submitting the form I can create a new User instance with the same username as the one has that I am looking for. Then in the DaoImpl class I can query for the “old” User which shares the userName with the newly created one. But I guess it is quite wasteful and ugly so I can’t believe that there is no better way.

UPDATE

   <!-- language: lang-java -->

 @RequestMapping(value = "/requestForFriends", method = RequestMethod.POST)
        public ModelAndView requestNewFriend(@ModelAttribute("user") User user, BindingResult result) {
            System.out.println(user.getUserName());
                System.out.println(user.getEmail());
                Map<String, Object> model = new HashMap<String, Object>();
                model.put("USERS", userService.getUsers());
                ModelAndView ret=new ModelAndView("addFriend", model);
                ret.addObject("user", new User());

                return ret;

            }
  • 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-06-10T05:32:16+00:00Added an answer on June 10, 2026 at 5:32 am
      @ModelAttribute
        public void fillUsers(Model model) {
            model.addAttribute("USERS", userService.getUsers());
        }
    
        @ModelAttribute
        public void getUser(@RequestParam(required = false) String userName, Model model) {
            User user = null;
            if (userName != null) {
                user = userService.getUserByName(userName);
            }
            if (user == null) {
                user = new User();
            }
            model.addAttribute("user", user);
        }
    
        @RequestMapping("/toAddFriend")
        public String toAddNewFriend() {
            return "addFriend";
    
        }
    
        @RequestMapping(value = "/requestForFriends", method = RequestMethod.POST)
        public String requestNewFriend(@ModelAttribute("user") User user) {
            System.out.println(user.getUserName());
            System.out.println(user.getEmail());
            return "addFriend";
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form made in JSP, here I have multiple buttons - Approve,
Here is the test.jsp <%@ page import=java.util.* %> <%@ page import=java.io.* %> <% String
Testing environment: netbeans7.11 + glassfish 3.11 here is the jsp, class code: <%@page contentType=text/html
I'm looking to use jScrollPane's scrollToElement API function here: http://dextersgospel.com/index-test.html Instructions on it's usage
Here is the config from shiro.ini shiro.loginUrl = /login.jsp ######### URL CONFIG ################### [urls]
Still learning JSP Web Applications here. I have been doing this for a while
In my JSP page, a tag <jsp:include page=servletName /> has been added. Here servletName
I'm developing a liferay portlet. Here is my code in jsp file: <table class=DDGridView>
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
I have written a jsp redirect in my jsp page like this <s:if test=#session.Doctor

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.