(1)通过href属性可以跳到一个jsp/html页面,也可以跳到action中。
a:跳到jsp页面(其中 jsp/test.jsp 是你的jsp的相对路径)
b:跳到action中(其中test是你的action在struts.xml中配置的name )
(2)如果跳转到action的参数需要从当前的页面中获取,这时可以通过增加事件来实现:οnclick=getvalue()
例如:你想将标签中的value值传到你的action中
这是你的 标签:
这是你的标签 //onclick调用js中函数即可
下面javascript里的函数
function getvalue()
{
//test为上述 标签的id
var myvalue = document.getElementByIdx_x("test").value;
//test1为上述标签的id;test2是你的action在struts.xml中配置的name
document.getElementByIdx_x("test1").href = "test2.action?id="+myvalue;
}
这种使用方式,说明使用的是get方式传递参数,因为使用了?key=value。在后端,使用request.getParameter()方法取值。
好奇,如果使用button的方式,怎么把参数传递到后端呢?这里没有form,没法post呀。
如果使用button,也可以利用上面的方式,构造一段url,使用get的方式,传递参数。
function quzhi() {
var text1 = document.getElementById("name").value; //取得文本框的值
var text2 = document.getElementById("password").value;
alert(text1);//网页提示框
alert(text2);
var myurl = "Test_01" + "?" + "pname=" + text1 + "&password=" + text2;
window.location.assign(encodeURI(myurl))
}
function quzhi2() {
var text1 = document.getElementById("name").value; //
var text2 = document.getElementById("password").value;
var myurl = "hellow.jsp" + "?" + "pname=" + text1 + "&password="+ text2;
window.location.assign(encodeURI(myurl))
}
后端
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
String strName=request.getParameter("pname");
String strPassword=request.getParameter("password");
System.out.println(strName);
System.out.println(strPassword);
}
参考资料:
JSP<A>标签的href属性
http://blog.sina.com.cn/s/blog_b291c26c0101arar.html
使用html <a href=""/>标签连接action的方法
https://www.cnblogs.com/plzdaye/p/3961250.html
js实现点击按钮传值
https://www.cnblogs.com/renxiuxing/p/8904211.html