当前位置:   article > 正文

struts 获取request_struts getrequest指定文字code

struts getrequest指定文字code

当初没有添加获取request的代码,所以总是反应空指针异常


下面我就给出一个实例程序来演示如何处理有多个submit的form:

【第1步】实现主页面(more_submit.jsp)


  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%@ taglib prefix="s" uri="/struts-tags"%>
  3. <html>
  4. <head>
  5.  <title></title>
  6. </head>
  7. <body>
  8.  <s:form action="submit.action" >
  9. <s:textfield name="msg" label="请输入内容"></s:textfield>
  10. <s:submit name="save" method="save" value="保存"/>
  11.   <s:submit name="print" method="print" value="打印" />
  12.  </s:form>
  13.  </body>
  14.  </html>

在more_submit.jsp中有两个submit:保存和打印。其中分别通过method属性指定了要调用的方法:save和print。因此,在Action类中必须要有save和print方法。


【第2步】实现Action类(MoreSubmitAction)

  1. package action;
  2. import javax.servlet.http.HttpServletRequest;
  3. import org.apache.struts2.ServletActionContext;
  4. import com.opensymphony.xwork2.ActionContext;
  5. import com.opensymphony.xwork2.ActionSupport;
  6. public class FirstAction extends ActionSupport {
  7. private String msg;
  8. public String getMsg() {
  9. return msg;
  10. }
  11. public void setMsg(String msg) {
  12. this.msg = msg;
  13. }
  14. // 处理savesubmit按钮动作
  15. public String save() throws Exception {
  16. HttpServletRequest request=ServletActionContext.getRequest();
  17. request.setAttribute("result", "成功保存[" + msg + "]");
  18. return "save";
  19. }
  20. // 处理printsubmit按钮动作
  21. public String print() throws Exception {
  22. HttpServletRequest request=ServletActionContext.getRequest();
  23. request.setAttribute("result", "成功打印["+msg+"]");
  24. return "print";
  25. }
  26. }


【第3步】配置Struts2 Action

struts.xml的代码如下:


  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <package name="demo" extends="struts-default">
  7. <action name="submit" class="action.FirstAction">
  8. <result name="save">/result.jsp</result>
  9. <result name="print">/result.jsp</result>
  10. </action>
  11. </package>
  12. </struts>

【第4步】编写结果页(result.jsp)

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'result.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <h1>${result }</h1>
  22. </body>
  23. </html>





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/259717
推荐阅读
相关标签
  

闽ICP备14008679号