使用Spring MVC控制器映射JSP文本输入

我正在开发一个简单的Java Web应用程序,该应用程序允许用户在文本框中输入一些信息,并在提交输入的文本时在另一个页面(success.jsp)中回显。我在StackOverflow中已经提到了许多教程和问题,但是无法完成。你能帮我这个忙吗?


我想将文本从jsp中的表单提交到SpringMVC控制器。Maven用于依赖项管理。


非常感谢


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>


四季花海
浏览 161回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java