Struts2—Action
二、命名空间namespace
·命名空间namespace须要放在相应的package下
·Namespace必须以“/开头”。
·Result的name是“success”的<result>能够不写其name。即:
<result name=”success”>与<result>效果同样。
·Namespace为空的情形是:
当找到url找到相应的namespace下的action时,假设资源没有找到。那么就能够使用na mespace为空的namespace下相应的action。
范例:
1.新建一个名叫Namespace的Web Project。配置当中的web.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
2.配置当中的Struts.xml,配置namespace=“”的场景
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>
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false"
/> <constant name="struts.devMode" value="true" /> <package name="default"
namespace="/" extends="struts-default"> <default-action-ref name="index"
/> <global-results> <result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception"
result="error"/> </global-exception-mappings> <action name="index"> <result
type="redirectAction"> <param name="actionName">HelloWorld</param> <param
name="namespace">/example</param> </result> </action> </package> <include
file="example.xml"/> -->
<!-- Add packages here -->
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="helloworld">
<result>
/Hello.jsp
</result>
</action>
</package>
<package name="namespacepackage" namespace="" extends="struts-default">
<action name="index">
<result>
/index.jsp
</result>
</action>
</package>
</struts>
3.编写index.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
this is index jsp <br>
</body>
</html>
4.
訪问namepace=”/”下的名为index的页面(没有该页面)
三、Action
·Action中的class的含义
class能够是随意的一个Java类。其组成为:
·返回值为String的方法,比如 execute()方法
·该class的实例化对象是在每次訪问的时候,都会创建一个新的对象出来。
当该对象创建 之后,就能够使用其execute()方法。然后依据其返回值,訪问对应的result
定义class的方法:
(1)直接使用POJO
定义一个Java类,定义当中的execute()方法
(2)实现Action接口
在当中实现execute()方法
(3)继承ActionSupport
·当class属性不存在的时候,默认使用的是x-work中的ActionSupport类,该类的execute ()方法默认返回的是success
·在实际应用中。指选择第三种继承ActionSupport类的方法来实现所须要的class
范例:
1.配置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>
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false"
/> <constant name="struts.devMode" value="true" /> <package name="default"
namespace="/" extends="struts-default"> <default-action-ref name="index"
/> <global-results> <result name="error">/WEB-INF/jsp/error.jsp</result>
</global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception"
result="error"/> </global-exception-mappings> <action name="index"> <result
type="redirectAction"> <param name="actionName">HelloWorld</param> <param
name="namespace">/example</param> </result> </action> </package> <include
file="example.xml"/> -->
<!-- Add packages here -->
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/actionclass" extends="struts-default">
<action name="action" class="com.zgy.action.Action">
<result name="success">
/Hello.jsp
</result>
</action>
</package>
</struts>
2.创建Action类。该类仅仅有一个execute()方法
package com.zgy.action;
import com.opensymphony.xwork2.ActionSupport;
public class Action extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
public String execute(){
return "success";
}
}
3.编写Hello.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>
<base href="<%=basePath%>">
<title>My JSP 'Hello.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is Hello Jsp <br>
</body>
</html>
4.浏览器端訪问
http://localhost:8080/ActionClass/actionclass/action
四、路径问题
·当訪问的namespace不存在的时候。就直接会在web.xml中进行处理。
在web.xml中配置 的<welcome-file>中配置的页面将会显示到浏览器。
·struts 2 中的路径是依据action的路径而不是jsp的路径来确定的,所以尽量不要使用相 对路径
·假设仅仅是在映射页面的URL中使用绝对路径,还是不能准确定位页面的位置
·解决方法:
在页面中,使用例如以下的配置
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
request.getContextPath()得到的路径是整个project的名
request.getScheme()得到的是协议如:http
request.getServerName()得到的是server地址如localhost
request.getServerPort()得到的是port号如:8080
·在写URL的绝对路径的时候,在前面加上<%=basepath%>
·使用<base href="<%=basePath%>">,指定base
范例:
1.配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?
>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="path" extends="struts-default" namespace="/path">
<action name="path" class="com.zgy.path.PathAction">
<result name="path">/path.jsp</result>
</action>
</package>
</struts>
2.编写PathAction
package com.zgy.path;
public class PathAction {
public String execute() {
return "path";
}
}
3.编写index.jsp
<?xml version="1.0" encoding="GB18030" ?
>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%--
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//在head中<base href>指定basePath
--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
<a href="path/path">路径问题说明</a>
</body>
</html>
4.编写path.jsp
<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
struts2中的路径问题是依据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。<br />
<a href="index.jsp">index.jsp</a>
<br />
尽管能够用redirect方式解决,但redirect方式并不是必要。
<br />
解决的方法很easy,统一使用绝对路径。
(在jsp中用request.getContextRoot方式来拿到webapp的路径)
<br />
或者使用myeclipse经经常使用的,指定basePath
</body>
</html>
5.浏览器訪问index.jsp页面
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFndWFuemhvdTIwMTQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
Struts2—Action的更多相关文章
- struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
- struts2 action 页面跳转
struts2 action 页面跳转 标签: actionstruts2redirect 2013-11-06 16:22 20148人阅读 评论(0) 收藏 举报 (1)type="di ...
- Java Hour 32 Weather ( 5 ) struts2 – Action class
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 32 Struts2 Action 1 将action 映射到 ac ...
- Struts2 Action接收表单参数
struts2 Action获取表单传值 1.通过属性驱动式 JSP: <form action="sys/login.action" method ...
- struts2 action result type类型
struts2 action result type类型 1.chain:用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息. com.opensymphony. ...
- struts2 action通配符
首先,看一个struts2的配置文件: <package name="actions" extends="struts-default" namespac ...
- 第三章Struts2 Action中动态方法调用、通配符的使用
01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...
- [Struts2] Action Implements SessionAware
struts2 的Action中若希望访问Session对象,可采用两种方式: 1.从ActionContext中获取: 2.实现SessionAware接口. 1.从ActionContext中获取 ...
- Struts2 Action下面的Method调用方法
1. 在struts.xml中加入<constant name="struts.enable.DynamicMethodInvocation" value="tru ...
- Struts2 Action中动态方法调用、通配符的使用
一.Struts2执行过程图: 二.struts2配置文件的加载顺序 struts-default.xml---struts-plugin.xml---struts.xml 具体步骤: 三.Actio ...
随机推荐
- BZOJ 2001 线段树+LCT (TLE)
同是O(nlog^2n)怎么常数差距就这么大呢,,, 同是LCT 怎么我的和Po姐姐的常数差距就这么大呢 我绝对是脑子被驴踢了才写这个垃圾算法 //By SiriusRen #include < ...
- Codeforces 803G Periodic RMQ Problem ST表+动态开节点线段树
思路: (我也不知道这是不是正解) ST表预处理出来原数列的两点之间的min 再搞一个动态开节点线段树 节点记录ans 和标记 lazy=-1 当前节点的ans可用 lazy=0 没被覆盖过 els ...
- Codeforces 769C
很久没有发题解,今天这题卡了下百度没看到相关题解,最后还是看了官方题解才找到原本思路的bug过的. 题意:给出一个二维迷宫,*表示墙,. 表示路,X表示起点,问一个长度为k的路径,从X出发并且回到X, ...
- [转]发布基于T4模板引擎的代码生成器[Kalman Studio]
本文转自:http://www.cnblogs.com/lingyun_k/archive/2010/05/08/1730771.html 自己空闲时间写的一个代码生成器,基于T4模板引擎的,也不仅是 ...
- iOS图片瘦身总结
前言 最近在公司写了个小程序来为iOS应用中的图片瘦身,进而减小APP大小,减少用户下载时的流量. 瘦身是在一个专门为图片瘦身的网站进行的. 地址:https://tinypng.com 这个网站提供 ...
- FullCalendar日程设置
顺序很重要!!!不然会报错,后面的文件会引用前面的一些东西. shili1: http://blog.csdn.net/lizai22/article/details/53522523 shili ...
- 联想 K5 Note(L38012)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI3.9.218
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- Python初学1
windows版python下载: https://pan.baidu.com/s/1dsAPp0C9PJUF73kFDdAzXQ 安装时勾选pip和Add python.exe to Path. w ...
- CentOS 7 使用 yum 安装 MariaDB 与 MariaDB 的简单配置
闲置已久的空间环境配置忘得差不多了,今天得空整理,重置了磁盘重新搭建环境,首先安装MariaDB的数据库,在这里记录下安装过程,以便以后查看. 1.安装MariaDB 安装命令 yum -y inst ...
- react 子组件给父组件传值
import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child from ' ...