【JAVAEE】Struts2-Action访问Web资源
本文最后更新于 1739 天前,其中的信息可能已经有所发展或是发生改变。

一.利用ServletActionContex访问Web对象–Servlet依赖容器方式。
1.设计Action。
com.action/Addaction.java
[sourcecode language=”java” title=”com.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;
}

}[/sourcecode]
2.jsp页面
input.jsp
[sourcecode language=”html” title=”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>[/sourcecode]
negative.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>计算结果为负数的页面</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>[/sourcecode]
3.配置xml。
web.xml
[sourcecode language=”xml” title=”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>[/sourcecode]
struts.xml
[sourcecode language=”xml” title=”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>
[/sourcecode]

项目下载:[bdbtn]https://pan.benzhu.xyz/%E4%BB%A3%E7%A0%81/%E6%BA%90%E4%BB%A3%E7%A0%81/%E9%A1%B9%E7%9B%AE/%E5%88%A9%E7%94%A8ServletActionContex%E8%AE%BF%E9%97%AEWeb%E5%AF%B9%E8%B1%A1%E2%80%93Servlet%E4%BE%9D%E8%B5%96%E5%AE%B9%E5%99%A8%E6%96%B9%E5%BC%8F.rar[/bdbtn]
二.通过ActonContext访问–Map依赖容器方式(与Servlet API解耦访问方式)
修改Addaction.java文件即可
com.action/Addaction.java
[sourcecode language=”java” title=”com.action/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;
}

}[/sourcecode]
项目下载:[bdbtn]https://pan.benzhu.xyz/%E4%BB%A3%E7%A0%81/%E6%BA%90%E4%BB%A3%E7%A0%81/%E9%A1%B9%E7%9B%AE/%E9%80%9A%E8%BF%87ActonContext%E8%AE%BF%E9%97%AE–Map%E4%BE%9D%E8%B5%96%E5%AE%B9%E5%99%A8%E6%96%B9%E5%BC%8F.rar[/bdbtn]

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇