web-inf / jsp页面执行 ---web.xml过滤器 ---->struts.xml指定由哪个java类来处理 -->src/java处理->默认执行execute()方法并反回SUCCESS,该方法可以重载用来分配由哪一页面显示结果
值由jsp的value值跟java文件中的get...作对应 如sum 对应getSum()复到计算后的值
sum.jsp 第一页
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <s:form action="/mystruts/sum.action"> 操作数1<s:textfield name="operand1"/> 操作数<s:textfield name="operand2"/> <s:submit value="代数和"/> </s:form> </html>
negative.jsp 返回结果页1
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <s:property value="sum"/> <h1>negative.jsp</h1> </html>
positive.jsp 返回结果页2
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <s:property value="sum"/> <h1>positive.jsp</h1> </html>
struts.xml 配置项
<package name="struts2" namespace="/mystruts" extends="struts-default"> <action name="sum" class="action.FirstAction"> <result name="negative">/negative.jsp</result> <result>/positive.jsp</result> </action> </package>
FirstAction.java
package action; import com.opensymphony.xwork2.ActionSupport; public class FirstAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private int operand1; private int operand2; public void setOperand1(int operand1) { this.operand1 = operand1; } public int getOperand1() { return operand1; } public void setOperand2(int operand2) { this.operand2 = operand2; } public int getOperand2() { return operand2; } public String execute() throws Exception { if(getSum()>=0) { return SUCCESS; }else { return "negative"; } } public int getSum() { return operand1+operand2; } }