3

i am working on a Spring WebApplication. I have a Form with just one input, a textline. I want to get that text and use it somewhere else.

So the question is pretty simple: Is there any way, i can use @ModelAttribute String text on the controller method to avoid using a helper class?

So i want to do this:

  @RequestMapping(value="/url", method = RequestMethod.POST)
         public String handler(@ModelAttribute("inputtext") String inputtext, Model model)
        {
            sout(inputtext);
}

With a form like this:

    <form:form method="POST" action="url" commandName="inputtext">
   <table>
    <tr>
        <td><form:label path="???">The text</form:label></td>
        <td><form:input path="???"/></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>  
</form:form>

If that's possible, what would i use for "path"?

Thanks!

1 Answer 1

4

Your question isn't clear, but the path= is intended to set property to a model class where your form get bound to. If you don't do any binding activities from that form to a model class, instead of you just want to pass the string, then don't use @ModelAttribute but @RequestParam instead,

@RequestMapping(value="/url", method = RequestMethod.POST)
public ModelAndView takeTheString( @RequestParam("inputtext") String inputtext) {    
           String temp_input =inputtext; 

       // By "sout" you mean "System.out.println()" right?
        System.out.println(temp_input );
    }

or with request object

@RequestMapping(value="/url", method = RequestMethod.POST)
public ModelAndView takeTheString(HttpServletRequest request, HttpServletResponse response) {               
          String temp_input =request.getParameter("inputtext"); 
           System.out.println(temp_input );   
    }

And The @ModelAttribute is supposed to annotated to a bound model like :

@ModelAttribute("userForm") UserBean user

This user is what I meant by the model class.

As requested under comment :

enter image description here

web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>

springmvc-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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd
                           http://www.springframework.org/schema/mvc 
                           http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <context:component-scan base-package="com" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

index.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="output.html" method="post">
            <input type="text" name="yourtext" value="">
            <input type="submit"  value="go">           
        </form>
    </body>
</html>

output.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
       text = ${name}     
    </body>
</html>

InputController.java :

package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class InputController {
    @RequestMapping(value = "/output", method = RequestMethod.POST)
    public ModelAndView takeTheString(@RequestParam("yourtext") String name) {   
        System.out.println("String you've passed = "+name);
       return new ModelAndView("output", "name", name);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hello and thank you for your answer. I think you got my question right :) I already thought about using @RequestParam but i do not understand what to do in the JSP then. As far as i understand @RequestParam gets Parameters from the URL right? So how do i pass the contents of my input field to the Controller? (And sout = System.out.println() yes :) )
If there's no binding activity (that you mean by helper class), then don't use <form:..> prefix in front of those tags , just use normal HTML tags, ex: <input type="text" name="myname" value="" /> , then in controller catch it up with @RequestParam("myname") String myName then output this myName (note that the ouput is shown in server's console, Tomcat, Glassfish or whatever you're using there), because if you use <form:..> hence the "path=" is mandatory, while no binding there (since the path= has to refer the binder class's attribute ).
Thank you.. Well that's quite simple after all :D For some reason i just never thought about not using the <form:..>. If you want you can post that as answer/add that to your answer so i can mark it.
Alright, deal taken :P. There will be 2 outputs (one is in server's console and another one is in output.jsp page), and done.. , you're welcome.
Wow now you really earned that with your detailed answer ;)

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.