struts2的namespace问题

       namespace决定了action的访问路径,默认为"",可以接受所有路径的action

  namespace可以写为/,或者/xxx,或者/xxx/yyy,对应的action访问路径为/index.action,/xxx/index.action,或

者/xxx/yyy/index.action

  namespace最好也用模块来进行命名

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="front" namespace="/" extends="struts-default">
        <action name="index">
            <result name="success">/Namespace.jsp</result>
        </action>
    </package>

  <package name="main" extends="struts-default" namespace="">
        <action name="index">
            <result>/Namespace.jsp</result>
        </action>
    </package>
</struts>

注解:

    package:用来区分重名的情况

    namespace:必须用"/"开头,如:/front ,在url中我们就要输入:http://localost:1000/struts2_0200_Namespace/front/index.action

  package和namespace在开发的时候都以模块来命名。

 Namespace.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>
namespace决定了action的访问路径,默认为"",可以接收所有路径的action<br />
namespace可以写为/,或者/xxx,或者/xxx/yyy,对应的action访问路径为/index.action,
/xxx/index.action,或者/xxx/yyy/index.action.<br/>
namespace最好也用模块来进行命名
</body>
</html>

如果在struts.xml中设定某action的命名空间 namespace="/test",如:

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

如果在根目录的jsp文件(如 /login.jsp)中将s:form标签的指向某action的地址设为:

<s:form action="test/login.action">

则生成的相应页面代码为:

<form id="login" onsubmit="return true;" action="test/login.action" method="post">

当多次执行这一页面时(如每次登陆验证都不通过),则浏览器url地址中的namespace将会重复出现,如:

http://localhost:8080/webAppName/test/test/test/test/login.action

而如果将s:form标签中的指向某action的地址设为:

<s:form action="/test/login.action">

生成的相应页面代码为:

<form id="login" onsubmit="return true;" action="/webAppName/test/login.action" method="post">

此时多次执行这一页面url地址中的namespace也不会重复出现,浏览器url地址为

http://localhost:8080/webAppName/test/login.action

运行正常

文章出处:http://www.diybl.com/course/1_web/webjs/2008224/101765.html

=========================================

如果不写namespace属性:

<package name="mywork" extends="struts-default">

则默认为“”空串,如果找不到url请求中命名空间下匹配的action时候,最后都还要在这个默认的命名空间里面找看看有没有匹配的action,在找不到才报错。

我们只要严格按照http://webserver/webPrefix/namespace/actionname.action这个规则来配置url肯定是不是错的咯

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值