问题描述:

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!

网友答案:

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 :

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);
    }
}
相关阅读:
Top