开发者

Passing value of a java method to a javascript variable using Struts 2 tag

开发者 https://www.devze.com 2023-02-12 22:53 出处:网络
I have a JSP file <%@ taglib prefix=\"s\" uri=\"/struts-tags\"%> <% response.setContentType(\"application/javascript\"); %>

I have a JSP file

<%@ taglib prefix="s" uri="/struts-tags"%>
<% response.setContentType("application/javascript"); %>

var collegename = '<s:text name="student.collegename"/>';

student.collegename defined in messages.properties file. now i can use collegename variable in java script file.

Is there any tag in Struts2 by using those i can pass the java value to a java 开发者_StackOverflow中文版script variable ?

for example:

var jsvariable = 'string value of a java method';

I have used this but its not worked: Servlet code: HttpServletRequest request = ServletActionContext.getRequest();

request.setAttribute("collegename", collegename);

JSP code:var collegename= '<%=request.getAttribute("collegename")%>'; Java Script code:alert("collegename"+collegename)

it show on IE:'collegename' is undefined

please suggest

i have also try this: Action class: import javax.servlet.http.HttpServletRequest; HttpServletRequest request; HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("collegename", collegename); System.out.println("now getting attribute-----"); String attr=(String) request.getAttribute("cname"); System.out.println("attr is:::"+attr);

getter/setter for collegename property.

JSP code:<%@ taglib prefix="s" uri="/struts-tags"%> <% response.setContentType("application/javascript"); %> var collegename= '<%=request.getAttribute("collegename")%>';

Java Script code:alert("collegename"+collegename) it show on IE: 'collegename' is undefined

although it show getting attribute (college name) on console but it is not show on java script alert.

it show on IE: 'collegename' is undefined

please review.


You can use some like code.

var jsvariable = '<%=request.getAttribute("testattribute")%>';

Hope it helps


This will call the getSomeValue() method on your action class.

var collegename = '<s:property value="someValue" escapeJavaScript="true"/>';


How about using EL like ${userName} or OGNL %{userName}...

0

精彩评论

暂无评论...
验证码 换一张
取 消