Struts2关于Action的系统训练3关于Action创建和path路径问题

1有时候我们需要自己创建Action来决定返回具体的视图,下面我就来演示一下怎么创建一个Action

2首先在struts配置文件中定义Action和对应的视图,例如:

<?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="front" extends="struts-default" namespace="/">
        <action name="index" class="com.bjsxt.struts2.front.action.IndexAction1">
            <result name="success">/ActionIntroduction.jsp</result>
        </action>
    </package>

</struts>

我们添加了一个action,叫做IndexAction1,结果对应相应的jsp文件,下面我们创建IndexAction1.java文件,其中有三种方法,我只讲最常用的一种如下:

package com.struts2.front.action;

import com.opensymphony.xwork2.ActionSupport;

public class IndexAction1 extends ActionSupport {
	
	@Override
	public String execute() {
		return "success";
	}
}
通过使用继承ActionSupport的方法好处是可以直接使用后者的方法。建议这样用最好。下面是对应的jsp文件

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
<!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>
最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法<br />
</body>
</html>
这样就实现了自己创建action的过程,但是通过action来控制视图的过程中,关于path路径的设置要牢记,特别是多个action多个jsp文件时,使用绝对路径,避免出错,下面给出代码和解释:

这是struts配置文件:

<?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.bjsxt.struts2.path.action.PathAction">
            <result name="path">/path.jsp</result>
        </action>
    </package>
</struts>
这是对应的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 />
解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
<br />
或者使用myeclipse经常用的,指定basePath
</body>
</html>
在action中路径为<result name="path">/path.jsp</result>,要是存在多个/path.jsp就会出错,所以使用绝对路径,在jsp中使用request.getContextRoot方式来拿到webapp的路径,指定basepath,通过<base href="<%=basePath%>" />来解决路径问题。

期待下次讲解





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值