我希望通过标记“a”将查看器中的值发送到控制器,而不使用“form”标记。
在我的观察中,我编码:
<input type="text" id="stuid"/>
<input type="text" id="stuname"/>
<a href="student.html? //something here that I don't know...
并且在Controller中我应该怎么做。
我不知道是否可以使用标签将值从查看器传递给控制器。拜托,帮帮我...谢谢......
答案 0 :(得分: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>
答案 1 :(得分: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
}
答案 2 :(得分:0)
我建议您使用ajax和JQUERY,例如我认为您必须在不使用表单标记的情况下将3个参数从JSP传递给SERVLET
的index.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>
APP-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);
}
}
userBean.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>