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中添加配置
<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>
lib目录下添加jar包
-
asm-x.x.jar
-
asm-commons-x.x.jar
-
asm-tree-x.x.jar
-
commons-io-X.X.X.jar
-
commons-lang3-X.X.X.jar
-
commons-fileupload-X.X.X.jar
-
freemarker-X.X.X.jar
-
javassist-X.X.X.jar
-
ognl-X.X.X.jar
-
struts2-core-X.X.X.X.jar
-
xwork-core-X.X.X.jar
java文件
package com.umgsai.test; import com.opensymphony.xwork2.ActionSupport; publicclass FirstAction extends ActionSupport { /** * */ privatestaticfinallong serialVersionUID = 1L; privateint operand1; privateint operand2; public String execute() throws Exception { if (getSum() >= 0) // 如果代码数和是非负整数,跳到positive.jsp页面 { return"positive"; } else// 如果代码数和是负整数,跳到negative.jsp页面 { return"negative"; } } publicint getOperand1() { return operand1; } publicvoid setOperand1(int operand1) { System.out.println(operand1); this.operand1 = operand1; } publicint getOperand2() { return operand2; } publicvoid setOperand2(int operand2) { System.out.println(operand2); this.operand2 = operand2; } publicint getSum() { return operand1 + operand2; // 计算两个整数的代码数和 } }
sum.jsp文件
<%@ page language="java"import="java.util.*"pageEncoding="GBK"%> <%@ taglib prefix="s"uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <basehref="<%=basePath%>"> <title>输入操作数</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 求代数和 <br/> <s:formaction="sum"namespace="/"> <s:textfieldname="operand1"label=" 操作数1"/> <s:textfieldname="operand2"label=" 操作数2"/> <s:submitvalue="代数和"/> </s:form> </body> </html>
positive.jsp页面
<%@ page language="java"import="java.util.*"pageEncoding="GBK"%> <%@ taglib prefix="s"uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <basehref="<%=basePath%>"> <title>显示代数和</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 代数和为非负整数 <h1> <s:propertyvalue="sum"/> </h1> </body> </html>
negative.jsp页面
<%@ page language="java"import="java.util.*"pageEncoding="GBK"%> <%@ taglib prefix="s"uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <basehref="<%=basePath%>"> <title>显示代数和</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 代数和为负整数 <h1> <s:propertyvalue="sum"/> </h1> </body> </html>
------------------------------------------------------------------------------