设计一个简单的web程序,其功能是让用户输入两个整数,并提交给Action,在Action中设计这两个数的代数和,如果代数和为非负数,则跳转到Positive.jsp页面,否则跳转到Negative.jsp页面。
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Struts</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>
设计控制类Action.jsp
package Action; public class Action { private int x; private int y; private int sum; public int getX(){ return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getSum() { return sum; } public String execute(){ sum=x+y; if(sum>=0)return "+"; else return "-"; } }
struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="add" class="Action.Action"> <result name="+">Positive.jsp</result> <result name="-">Negative.jsp</result> </action> </package> </struts>