将输入从文本框发送到控制器

时间:2014-02-04 12:06:35

标签: java spring spring-mvc

我想通过点击提交按钮将文本框中的输入发送到控制器。

以下是控制器代码...... 控制器:

package com.sms.app.controller;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import com.sms.app.util.FAQUtility;


public class FAQController extends MultiActionController{

    private FAQUtility faqUtility;

    public void setFaqUtility(FAQUtility faqUtility) {
        this.faqUtility = faqUtility;
    }



    public ModelAndView getHomePage(HttpServletRequest request,
            HttpServletResponse response) throws ServletException {
        System.out.println("In method getHomePage of FAQController");
        return new ModelAndView("home");
    }
}

这是jsp页面...... JSP:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"  contentType="text/html"%>  
<%@ taglib uri="/tld/c.tld" prefix="c" %>
<%@ taglib uri="/tld/fmt.tld" prefix="fmt" %>
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
String contextPath = request.getContextPath();   
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SMS FAQ</title>
<link href="css/stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/home/home.js"></script>
</head>
<body oncontextmenu="return false;" width="100%" height="100%">
    <form name="Home" action="" method="POST">
    <input type="hidden" id="contextPath" name="contextPath" value="<%=contextPath%>" />
        <table width="100%" align="center">
            <tr>
                <td width="100%" >
                <table width="100%">
                    <tr>
                        <td>FAQ SMS</td>
                        <td>&nbsp;</td>
                    </tr>
                     <tr>
                        <td width="25%" class="col_set"> <span class="normal_blue_text">SMS Question</span></td>
                        <td width="75%"><input type="text" id="smsQuestion" name="smsQuestion" class="normal_inputbox" value="" maxlength=30/></td>
                     </tr>
                </table>
                <table>
                    <tr>
                        <td>
                            <input type="button" id="okBtn" name="okBtn" class="blue_btn" value="Submit" />
                        </td>
                    </tr>
                </table>
                </td>
            </tr>
        </table>
    </form>   
  </body>
</html>

请告知如何继续。 教程上的任何链接都会有所帮助。

1 个答案:

答案 0 :(得分:0)

下面的代码将文本框值发送给控制器的操作。

<input id="txt" type="text">
<button id="button">Click Me</button>


<script type="text/javascript">
    $("#button").click(function () {
        var txtVal = $("#txt").val();
        window.location = "@Url.Action("TheAction","TheController")" + 
                          "/" + txtVal;
    });
</script>

或在javascript中

假设你的按钮是这样的:

<input type="button" id="btnSetText" value="set Text" onclick="setText"  />

那么你的js脚本应该是:

<script>
    function setText(){
            document.getElementById('txtBoxId').value = 'watever'; 
   }
</script>

现在,如果你想在给定的控制器中向actionmethod发送一个值而不是这样:

<script>
     window.href.location ='/ControllerName/ActionMethod?value=' + 1;
  </script>

其中ActionMethod是这样的:

public ActionResult ActionMethod(string value)
 {
       //process value
 }

注意:

<input type='text' name='UnboundTextBoxName' />

MVC将自动获取UnboundTextBoxName的值并将该值插入到同名参数中。

谢谢,

希瓦