jsp想js,action传值

1、struts2 action如何向JSP的JS函数传值

action中定义变量
public class TestAction extends ActionSupport implements ServletRequestAware {
    private String state = "test";
}

JSP的JS函数中引用变量
<script type=text/javascript>
    function getStatus() {
        var t = "${state}";
        alert(t);
    }
</script>

2、struts2 action如何向JSP传值

action中定义变量
public class TestAction extends ActionSupport implements ServletRequestAware {
    private String state = "test";
}

JSP中引用变量
<s:property value="#port.publicPort" />

3、JSP如何向struts2 action的函数传值

JSP中的值在form中通过submit的方式提交到action中,如下:
<form class="form-horizontal">
    <input type="text" name="state" id="state" onclick="doTest()"></input>
</form>

<script type=text/javascript>
    function doTest() {
        document.forms[0].method = "post";
        document.forms[0].action="<%=request.getContextPath()%>/TestManage_showTest";
        document.forms[0].submit();    
    }
</script>

action中获取此值
public String showTest() throws Exception {
    String state = (String)httpServletRequest.getParameter("state");
}

4、JSP如何向JS传值

JSP中定义的变量
<input type="text" name="state" id="state" value="test"></input>

JS中可以使用下面的方法引用变量的值
<script type=text/javascript>
var v = document.getElementById("state").value;
</script>

5、JSP定义和使用java变量

JSP中定义的java变量
<%
String userId = "test";
%>

JSP中可以使用下面的方法引用此变量
'<%=userId%>'

原文地址:https://www.cnblogs.com/zzyrunning/p/4561059.html