今天继续学习Struts2测试了个request.setAttribute 传值,刚开始一直不行,过了一会自己好了,郁闷
老是通过ACTION传到显示界面总是NULL,我改了好几道 最好自己好了,真是气死我了,贴出来供大家伙批评
需要解决用 OGNL语言显示结果
输入页面 代码
<%@ page language="java" contentType="text/html;charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib prefix="s"uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=GB18030">
<title>Insert titlehere</title>
</head>
<body>
<s:form action="submit.action">
<s:textfield name="msg"label="请输入名字"></s:textfield>
<s:submit name="print" value="打印"method="print"></s:submit>
<s:submit name="save" value="保存"method="save"></s:submit>
</s:form>
</body>
</html>
显示页面代码:
<%@ page language="java" contentType="text/html;charset=GB18030"
pageEncoding="GBK"%>
<%@taglib prefix="s"uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=GB18030">
<title>ShowInfo</title>
</head>
<body>
<%=request.getAttribute("result")%>
</body>
</html>
Action代码
package action;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class MoresubmitAction extends ActionSupport implementsServletRequestAware
{
private String msg;
private javax.servlet.http.HttpServletRequestrequest;
@Override
public void setServletRequest(HttpServletRequestrequest) {
// TODO Auto-generated methodstub
this.request=request;
}
public String print(){
request.setAttribute("result","成功打印["+msg+"]");
System.out.println(request.getAttribute("result"));
return "print";
}
public String save(){
request.setAttribute("result","成功保存["+msg+"]");
return "save";
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
web.xml代码
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>MyApplication</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//ApacheSoftware Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devmode"value="true"></constant>
<package name="submit"extends="struts-default">
<action name="submit">
<resultname="print">/result.jsp</result>
<resultname="save">/result.jsp</result>
</action>
</package>
</struts>
视图图片