struts2基本配置

来源:互联网 发布:linux mkdir -m 编辑:程序博客网 时间:2024/06/06 03:31

struts.xml 放在src目录下

<?xml version="1.0" encoding="UTF-8"?><struts>    <package name="struts2 " namespace="/" extends="struts-default">        <action name="sum" class="com.umgsai.test.FirstAction">            <result name="positive ">/positive.jsp</result>            <result name="negative ">/negative.jsp</result>        </action>    </package></struts>

web.xml中添加配置

  1. <filter>

  2. <filter-name>struts2</filter-name>

  3. <filter-class>

  4.        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter            

  5. </filter-class>

  6.    </filter>

  7. <filter-mapping>

  8. <filter-name>struts2</filter-name>

  9. <url-pattern>/*</url-pattern>

  10. </filter-mapping>

lib目录下添加jar包

  1. asm-x.x.jar

  2. asm-commons-x.x.jar

  3. asm-tree-x.x.jar

  4. commons-io-X.X.X.jar

  5. commons-lang3-X.X.X.jar

  6. commons-fileupload-X.X.X.jar

  7. freemarker-X.X.X.jar

  8. javassist-X.X.X.jar

  9. ognl-X.X.X.jar

  10. struts2-core-X.X.X.X.jar

  11. xwork-core-X.X.X.jar

java文件

  1. package com.umgsai.test;  

  2. import com.opensymphony.xwork2.ActionSupport;  

  3. publicclass FirstAction extends ActionSupport {  

  4. /**

  5.     *  

  6.     */

  7. privatestaticfinallong serialVersionUID = 1L;  

  8. privateint operand1;  

  9. privateint operand2;  

  10. public String execute() throws Exception {  

  11. if (getSum() >= 0) // 如果代码数和是非负整数,跳到positive.jsp页面

  12.        {  

  13. return"positive";  

  14.        } else// 如果代码数和是负整数,跳到negative.jsp页面

  15.        {  

  16. return"negative";  

  17.        }  

  18.    }  

  19. publicint getOperand1() {  

  20. return operand1;  

  21.    }  

  22. publicvoid setOperand1(int operand1) {  

  23.        System.out.println(operand1);  

  24. this.operand1 = operand1;  

  25.    }  

  26. publicint getOperand2() {  

  27. return operand2;  

  28.    }  

  29. publicvoid setOperand2(int operand2) {  

  30.        System.out.println(operand2);  

  31. this.operand2 = operand2;  

  32.    }  

  33. publicint getSum() {  

  34. return operand1 + operand2; // 计算两个整数的代码数和

  35.    }  

  36. }

sum.jsp文件

  1. <%@ page language="java"import="java.util.*"pageEncoding="GBK"%>

  2. <%@ taglib prefix="s"uri="/struts-tags"%>

  3. <%  

  4. String path = request.getContextPath();  

  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  

  6. %>

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

  8. <html>

  9. <head>

  10. <basehref="<%=basePath%>">

  11. <title>输入操作数</title>

  12. <metahttp-equiv="pragma"content="no-cache">

  13. <metahttp-equiv="cache-control"content="no-cache">

  14. <metahttp-equiv="expires"content="0">

  15. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

  16. <metahttp-equiv="description"content="This is my page">

  17. <!--

  18.    <link rel="stylesheet" type="text/css" href="styles.css">

  19.    -->

  20. </head>

  21. <body>

  22.        求代数和  

  23. <br/>

  24. <s:formaction="sum"namespace="/">

  25. <s:textfieldname="operand1"label=" 操作数1"/>

  26. <s:textfieldname="operand2"label=" 操作数2"/>

  27. <s:submitvalue="代数和"/>

  28. </s:form>

  29. </body>

  30. </html>

positive.jsp页面

  1. <%@ page language="java"import="java.util.*"pageEncoding="GBK"%>

  2. <%@ taglib prefix="s"uri="/struts-tags"%>

  3. <%  

  4. String path = request.getContextPath();  

  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  

  6. %>

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

  8. <html>

  9. <head>

  10. <basehref="<%=basePath%>">

  11. <title>显示代数和</title>

  12. <metahttp-equiv="pragma"content="no-cache">

  13. <metahttp-equiv="cache-control"content="no-cache">

  14. <metahttp-equiv="expires"content="0">

  15. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

  16. <metahttp-equiv="description"content="This is my page">

  17. <!--

  18.    <link rel="stylesheet" type="text/css" href="styles.css">

  19.    -->

  20. </head>

  21. <body>

  22.        代数和为非负整数  

  23. <h1>

  24. <s:propertyvalue="sum"/>

  25. </h1>

  26. </body>

  27. </html>

negative.jsp页面

  1. <%@ page language="java"import="java.util.*"pageEncoding="GBK"%>

  2. <%@ taglib prefix="s"uri="/struts-tags"%>

  3. <%  

  4. String path = request.getContextPath();  

  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  

  6. %>

  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

  8. <html>

  9. <head>

  10. <basehref="<%=basePath%>">

  11. <title>显示代数和</title>

  12. <metahttp-equiv="pragma"content="no-cache">

  13. <metahttp-equiv="cache-control"content="no-cache">

  14. <metahttp-equiv="expires"content="0">

  15. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

  16. <metahttp-equiv="description"content="This is my page">

  17. <!--

  18.    <link rel="stylesheet" type="text/css" href="styles.css">

  19.    -->

  20. </head>

  21. <body>

  22.        代数和为负整数  

  23. <h1>

  24. <s:propertyvalue="sum"/>

  25. </h1>

  26. </body>

  27. </html>

------------------------------------------------------------------------------


0 0