使用<a href="">

时间:2016-09-20 21:04:02

标签: javascript jsp

In the jsp, I have something like this:

<form id="updateform" name="updateform" method="post">
    <a href="javascript:changePassword()" ><b>change Password</b></font></a>
</form>

function changePassword()
{
    var urlString= "./WebApp?app=msgsrv&class=changepassword&id=" + <%=id%> ;
    popupWindow( urlString , 'ChangePassword', '600', '400', false, true);
}

changePassword is kind of servlet class, having
execute(HttpServletRequest, HttpServletResponse) method.

This is how my current code looks, but I wanted to get rid of id in Url due to security issue.

I tried to use both a hidden field and request.setAttribute(), and neither of them worked. I wanted to know if there is any other way we could pass a value to the Java class without a form submission as I'm using popupWindow in anchor tag.

2 个答案:

答案 0 :(得分:0)

首先,您无法使用锚标记在java中获取JavaScript值。最好的方法是路径id作为方法参数。

<form id="updateform" name="updateform" method="post">
            <a href="#" onclick="changePassword(<%=id%>);" ><b>change Password</b></font></a>
</form>

function changePassword(id)
{
    var urlString = "./WebApp?app=msgsrv&class=changepassword&id=" + id + "";
    popupWindow(urlString, 'ChangePassword', '600', '400', false, true);
}

答案 1 :(得分:0)

我在这里说了你的陈述。

  

我想知道是否还有其他方法可以在没有表单提交的情况下将值传递给Java类,因为我在锚标记中使用popupWindow。

我认为你假设你不能因标签而提交表格。但事实是你可以使用javascript使用锚标签提交表格。

$("form").submit();

完整代码示例

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("form").submit(function(){
        alert("Submitted");
    });
    $("#submitbutton").click(function(){
        $("form").submit();
    });
});
</script>
</head>
<body>


<form id="updateform" name="updateform" method="post">
   <input type="hidden" name="app" value="msgsrv">
   <input type="hidden" name="class" value="changepassword">
   <input type="hidden" name="id" value="1000">
   <a id="submitbutton" ><b>change Password</b></font></a>
</form>


</body>
</html>

您也可以使用AJAX表单提交。

jQuery.ajax({
    url:  '<c:url value="/WebApp" />',
    type: 'POST',
    dataType:'json',
    data: jQuery('#updateform').serialize(),
    success: yourSuccessFunctionNameHere
});