test.java
public class testAction implements Action {
}
test.jsp
================================================================
遍历页面request
for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();) {
Object o = e.nextElement();
out.println((String) o + " : " + request.getAttribute((String) o) + " <br><hr> " );
}
%>
1 : request.getAuthType() = null
2 : request.getCharacterEncoding() = null
3 : request.getContentLength() = -1
4 : request.getContentType() = null
5 : request.getContextPath() = /imageCaixun
6 : request.getMethod() = GET
7 : request.getPathInfo() = null
8 : request.getPathTranslated() = null
9 : request.getQueryString() = null
10 : request.getRealPath("/") = D:\project\bank_image\web\
11 : request.getRemoteAddr() = 127.0.0.1
12 : request.getRemoteHost() = 127.0.0.1
13 : request.getRemoteUser() = null
14 : request.getRequestURI() = /imageCaixun/demo.jsp
15 : request.getRequestURL() = http://localhost/imageCaixun/demo.jsp
16 : request.getServletPath() = /demo.jsp
17 : request.getServerName() = localhost
18 : request.getServerPort() = 80
================================================================
如果在Action类里有一个简单的变量,并且已经写好了它的get和set方法
private String test="jsp value test";
那么我们就可以在页面上直接获取,而不通过struts2或者webwork的标签.
首页jsp页面要引入JAR包
action所在的包 com.XXX.XXX
struts2包为org.apache.struts2.ServletActionContext
然后在页面上定义一个ServletActionContext变量
<%
HttpServletRequest request1 = ServletActionContext.getRequest();
String test=request1.getAttribute("test");
%>
然后输出就可以看到结果
<%=test%>
注:由于JSP本身已经有了自己的request对象,所以我们定义HttpServletRequest要重新选择一个名字,否则将会报错.