0

我想通过标签“a”将值从查看器发送到控制器,而不使用“form”标签。

在我的查看器中,我编码:

           <input type="text" id="stuid"/>
           <input type="text" id="stuname"/>

           <a href="student.html? //something here that I don't know...

在控制器中我应该如何处理它。

我不知道是否可以使用标签将值从查看器传递给控制器​​。请帮助我...谢谢...

4

3 回答 3

1

您可以从如下链接传递参数<a>

<a href="student.html?name=John&age=21">Student</a>

在控制器中,您可以映射请求参数,例如:

@RequestMapping("student")
public String student(@RequestParam("name") String name, 
                 @RequestParam("age") Integer age) {
  //...use name and age
}
于 2012-12-20T09:48:45.203 回答
1

你可以试试这个

<HTML> 
<HEAD> 
<SCRIPT language="JavaScript"> function test() 
{ var p=document.getElementById("stuid").value;
    window.open("sample.html?val="+p,'_self'); 
} 
</SCRIPT> 
</HEAD> 
<BODY>
<input type="text" id="stuid" name="stuid"/> 
<input type="text" id="stuname" name="stuname"/> 
<A HREF="javascript:test()">Test</a>
</BODY> 
</HTML>
于 2012-12-20T06:21:24.507 回答
0

我建议你使用 ajax 和 JQUERY,例如我认为你必须将 3 个参数从 JSP 传递给 SERVLET,而不使用表单标签

索引.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>Index Page</title>

<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
<script src="js/app-ajax.js" type="text/javascript"></script>
</head>
<body>

        Enter Your Name: <input type="text" id="name" /><br>
        Enter our user name :<input type="text" id="userName"><br>
        Enter your Password :<input type="password" id="password"><br>
        <input type="button" value="Submit" onclick="ajaxCall();">        
    <br>

    <strong> Response</strong>:
    <div id="ajaxGetUserServletResponse"></div><br>
    <div id="ajaxGetUserServletResponseusername"></div><br>
    <div id="ajaxGetUserServletResponsepassword"></div><br>
</body>
</html>

应用程序-ajax.js

   function ajaxCall(){
    var name = jQuery("#name").val(); 
    var userName = jQuery("#userName").val();
    var password= jQuery("#password").val();
    alert(name);
    alert(userName);
    alert(password);

    jQuery.ajax({
        url : "GetUserServlet", 
        method: "GET", 
        type : "JSON", 
        data : "name="+name+"&userName="+userName+"&password="+password,// query parameters 1st 
        success : function(response){
            $('#ajaxGetUserServletResponse').text(response);

        }
    });


}

GetUserServlet.java

package com.logic;

import java.io.IOException;

import java.util.ArrayList;

import javax.management.relation.RelationSupportMBean;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class GetUserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        userBean ub = new userBean();
        String name = request.getParameter("name").trim();
        String userName = request.getParameter("userName").trim();
        String password = request.getParameter("password").trim();

        System.out.println("name catched "+name);
        System.out.println("username catched"+userName);
        System.out.println("Password catched"+password);


        ArrayList<userBean> list = new ArrayList<userBean>();
         ub=new userBean();
         ub.setName(name);
         ub.setUserName(userName);
         ub.setPassword(password);
         list.add(ub);  

         response.setContentType("text/plain");
         response.getWriter().print(list);

    }

}

用户Bean.java

package com.logic;

public class userBean 
{
    private String name;
    private String userName;
    private String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>AjaxJspServlet</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

<servlet>
<servlet-name>one</servlet-name>
<servlet-class>com.logic.GetUserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/GetUserServlet</url-pattern>
</servlet-mapping>

</web-app>
于 2016-06-09T04:53:01.993 回答