一.Servlet依赖容器方式

利用ServletActionContex访问Web对象。

1.设计Action

Addaction.java

package com.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

public class Addaction {
    private HttpServletRequest request;
    private HttpSession session;
    private ServletContext application;
    private double x,y,sum;
    public Addaction(){
        request = ServletActionContext.getRequest();
        session = request.getSession();
        application = session.getServletContext();
    }
    public String execute(){
        sum=x+y;
        request.setAttribute("x2", x);
        session.setAttribute("y2", y);
        application.setAttribute("sum2", sum);
        if(sum>0){
            return "+";
        }else
        {
            return "-";
        }
    }

    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    public double getSum() {
        return sum;
    }
    public void setSum(double sum) {
        this.sum = sum;
    }

}

2.jsp页面

input.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>两数之和计算</title>
</head>
<body>
<form action="opadd" method="post">
<table>
<tr>
<th colspan="2">请输入两个实数:</th>
</tr>
<tr>
<td align="right">加数:</td>
<td><input type="text" name="x" /></td>
</tr>
<tr>
<td align="right">被加数:</td>
<td><input type="text" name="y" /></td>
</tr>
<tr>
<td align="right"><input type="submit" value="求和" /></td>
</tr>
</table>
</form>
</body>
</html>

negative.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计算结果为负数的页面</title>
</head>
<body>
代数和为负整数,其和值为:${request.x2}+${session.y2}=${application.sum2}
</body>
</html>[/sourcecode]
positive.jsp
[sourcecode language=”html” title=”negative.jsp”]
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计算结果为大于等于0的页面</title>
</head>
<body>
代数和为非负整数,其和值为:${request.x2}+${session.y2}=${application.sum2}<br>
</body>
</html>

3.配置xml

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>benzhu1</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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="default" namespace="/" extends="struts-default">

<action name="opadd" class="com.action.Addaction" method="execute">
<result name="+">positive.jsp</result>
<result name="-">/negative.jsp</result>
</action>

</package>
</struts>

二.Map依赖容器方式

通过ActonContext访问(与Servlet API解耦访问方式)。

1.修改java文件

修改Addaction.java文件即可

Addaction.java

package com.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

public class Addaction {
    private Map<String,Object>request;
    private Map<String,Object>session;
    private Map<String,Object>application;
    private double x,y,sum;
    @SuppressWarnings("unchecked")
    public Addaction(){
        ActionContext context = ActionContext.getContext();
        request = (Map<String,Object>)context.get("request");
        session = context.getSession();
        application = context.getApplication();
    }
    public String execute(){
        sum = x+y;
        request.put("x2", x);
        session.put("y2", y);
        application.put("sum2", sum);
        if(sum>=0)return "+";
        else return "-";
    }

    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    public double getSum() {
        return sum;
    }
    public void setSum(double sum) {
        this.sum = sum;
    }

}