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 3615472
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:22:40+00:00 2026-05-18T22:22:40+00:00

hi Trying to build my own spring application,I made a page to input name

  • 0

hi
Trying to build my own spring application,I made a page to input name and age.Clicking on Submit button should take me to another page that displays the user input.I decided not to use any database.As the first step ,I decided to create a controller with a method to show the form view.After I get this right,I would implement the other methods..

package personinfo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import personinfo.form.PersonInfo;

@Controller
public class PersonInfoController {  
@RequestMapping(value ="/addPerson" )
public ModelAndView showAddPersonForm(){
    return new ModelAndView("addperson", "personInfoEntries", new PersonInfo());
}

@RequestMapping(value ="/addPerson" ,method =RequestMethod.POST)
    public String addPersonInfo(@ModelAttribute("person")PersonInfo perInfo){
        String name = perInfo.getName();
        int age =  perInfo.getAge();
        System.out.println("Name:" + name +"Age:"+ age);
        return "showperson";//how to pass name and age
    }
...
}

form backing object is personinfo.form.PersonInfo class

package personinfo.form;

public class PersonInfo {
    private String name;
    private int age;

    public PersonInfo(){
    }

    public PersonInfo(String name,int age){
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

/WEB-INF/jsp/addperson.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>Add Person</title>
</head>
<body>
<h2>Add Person</h2>
<form:form action="addPerson.html" commandName="personInfoEntries">
<table>
<tr>
 <td>
 <form:label path="name">Name</form:label> 
 </td>
 <td>
 <form:input path="name"/>
 </td>
</tr>

<tr>
 <td>
 <form:label path="age">Age</form:label> 
 </td>
 <td>
 <form:input path="age"/>
 </td>
</tr>

<tr>
 <td colspan="2">
 <input type="submit" value="Add Person"/>
 </td>
</tr>
</table>
</form:form>
</body>
</html>

the perinfo/WebContent/index.jsp is

<%@ 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>index</title>
</head>
<body>
<jsp:forward page="addPerson.html"></jsp:forward>
</body>
</html>

/perinfo/WebContent/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>personinfo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

       <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>
   org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>*.html</url-pattern>
 </servlet-mapping>
</web-app>

finally the /perinfo/WebContent/WEB-INF/dispatcher-servlet.xml

<?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:annotation-config />

 <context:component-scan base-package="personinfo.controller" />

 <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
     p:prefix="/WEB-INF/jsp/"
        p:suffix=".jsp">

   <property name="contentType" value="text/html; charset=utf-8" />
 </bean>
</beans>

As chris pointed out,I added the @Controller to controller class and to the servlet.xml .
Now I am able to see the form.When I click on the submit button ,the showperson.jsp is displayed.So far so good.

When the addPersonInfo() method in PersonInfoController is executed I would like to pass the name and age values to showperson.jsp .How can I do this? I am returning a ‘showperson’ view name string.How can I add the arguments to it?And I couldn’t work out how to implement a corresponding method to which this viewname is to be mapped.

Can someone please show me?Or do I need to modify the return types of addPersonInfo() method.

Please help.

thanks,

mark

  • 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-18T22:22:40+00:00Added an answer on May 18, 2026 at 10:22 pm

    To answer your follow-up question,

    When the addPersonInfo() method in
    PersonInfoController is executed I
    would like to pass the name and age
    values to showperson.jsp .How can I do
    this? I am returning a ‘showperson’
    view name string.How can I add the
    arguments to it?And I couldn’t work
    out how to implement a corresponding
    method to which this viewname is to be
    mapped.

    You need to add the name and age values to the model and pass that to the view. You do it inside addPerson().

    @RequestMapping(value ="/addPerson" ,method =RequestMethod.POST)
    public String addPersonInfo(@ModelAttribute("person") PersonInfo perInfo, Model model) {
            String name = perInfo.getName();
            int age =  perInfo.getAge();
    
            // Add name reference to Model
            model.addAttribute("name", name);
            // Add age reference to Model
            model.addAttribute("age", age);
    
            return "showperson";
    }
    

    Then in your JSP page, you should at least have something like this:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!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=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
     
    <h1>Person</h1><table>
    <p>name: ${id} </p>
    <p>age: ${id} </p>
    </body>
    </html>
    

    Check my other tutorials on the link I provided. There’s plenty of examples like that

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build my own little toolbox for Vista. One of the
I'm trying to build my own template tags. I have no idea why I
I am trying to build my own boot loader that loads then switches from
I'm trying build an App Engine connected Android application and am having some problems
I'm trying to develop an online formbuilder. Each user can build their own forms
I am trying to build my own user authentication system (simply because the ones
I'm trying to understand the IntentService class so that I can build my own
Hey i'm trying to build an app in which the user can input some
I am trying to build an application on our company's intranet using ASP.NET and
I'm trying to build my own custom django form widgets (putting them in widgets.py

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.