root
root

Reputation: 177

Map JSP text input with Spring MVC controller

I'm developing a simple java webapp which lets user input some information in a textbox and when submitted the entered text, echoed in another page(success.jsp). I've referred number of tutorials and also questions here in StackOverflow, but couldn't manage to get this done. Could you please help me with this?

I want to submit text from a form in jsp to the SpringMVC controller. Maven is used for dependency management.

Thanks so much

mainController.java

import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;    
@Controller
public class mainController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Map<String, Object> model){
        textConv textconv = new textConv();
        model.put("textcovn", textconv);
        return "index";
    }

    @RequestMapping(value = "/translate", method = RequestMethod.POST)
    public String translate(@ModelAttribute("textconv") textConv textconv, Map<String, Object> model){
        System.out.println(textconv.getTextFrom());
        return "success";
    }
}

textConv.java

public class textConv {
    String textFrom;

    public String getTextFrom() {
        return textFrom;
    }

    public void setTextFrom(String textFrom) {
        this.textFrom = textFrom;
    }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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>Registration</title>
</head>
<body>
<h3>Enter text</h3>

 <form action="translate" method="POST" commandName="textconv">
 <textarea rows="4" cols="50" path="textFrom">Enter text here</textarea>
 <input type="submit" value="Echo">
</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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>Registration</title>
</head>
 <body>
    <h3>Text : ${textconv.getTextFrom}</h3>
    </body>
</html>

Upvotes: 0

Views: 2000

Answers (2)

Avijit Barua
Avijit Barua

Reputation: 3086

You forgot to bind the request parameter to model. Bind it to the model and pass it to the success view. Change your TestController as

@Controller
public class TestController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/translate", method = RequestMethod.POST)
    public String translate(@RequestParam String textForm, Model model) {
        // this will bind the request param to model
        model.addAttribute("textForm", textForm);

        return "success";
    }
}

You are also dealing an improper form to submit the request. If you are intended to use just a simple HTML form do it this way in index.jsp

   <form action="/translate" method="POST">
      <div class="form-group">
        <div class="col-md-4">
            <textarea class="form-control" name="textForm">
                   default text
            </textarea>
        </div>
      </div>
      <input type="submit" value="Submit">
   </form>

When you will submit the form and dispatch to success.jsp view use the correct expression to print the model values

<body>
    <h3>Text : ${textForm}</h3>
</body>

Upvotes: 2

lazyduckiy
lazyduckiy

Reputation: 301

I here share with you a simple to understand and workable solution.

EmployeeController.java

@Controller
public class EmployeeController {

    @RequestMapping(value = "/employee", method = RequestMethod.GET)
    public ModelAndView showForm() {
        return new ModelAndView("employeehome", "employee", new Employee());
    }

    @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
    public String submit(@Valid @ModelAttribute("employee") Employee employee, BindingResult result, ModelMap model) {
        if (result.hasErrors()) {
            return "employeeerror";
        }
        model.addAttribute("name", employee.getName());
        model.addAttribute("contactNumber", employee.getContactNumber());
        model.addAttribute("id", employee.getId());
        return "employeeview";
    }    
}

Employee.java

public class Employee {

    private String name;
    private long id;
    private String contactNumber;

    public Employee() {
        super();
    }

    public Employee(String name, long id, String contactNumber) {
        super();
        this.name = name;
        this.id = id;
        this.contactNumber = contactNumber;
    }

//getters and setters
}

employeehome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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>

    <h3>Welcome, Enter The Employee Details</h3>

    <form:form method="POST" action="${pageContext.request.contextPath}/addEmployee" modelAttribute="employee">
        <table>
            <tr>
                <td><form:label path="name">Name</form:label></td>
                <td><form:input path="name" /></td>
            </tr>
            <tr>
                <td><form:label path="id">Id</form:label></td>
                <td><form:input path="id" /></td>
            </tr>
            <tr>
                <td><form:label path="contactNumber">Contact Number</form:label></td>
                <td><form:input path="contactNumber" /></td>
            </tr>
            <tr>
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form:form>


</body>
</html>

employeeview.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!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>
    <h2>Submitted Employee Information</h2>
    <table>
        <tr>
            <td>Name :</td>
            <td>${name}</td>
        </tr>
        <tr>
            <td>ID :</td>
            <td>${id}</td>
        </tr>
        <tr>
            <td>Contact Number :</td>
            <td>${contactNumber}</td>
        </tr>
    </table>
</body>
</html>

employeeerror.jsp

<%@ 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>
    <h3>Pleas enter the correct details</h3>
    <table>
        <tr>
            <td><a href="employee">Retry</a></td>
        </tr>
    </table>

</body>
</html>

Upvotes: 0

Related Questions