一、假设建立一个action (引入的包省略)
ExampleAction.java
public class ExampleAction
{
public String userName;
public String execute(){
System.out.println("userName");
return "test";
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
二、struts.xml注册action,为了方便测试,取别名
//此处为有命名空间的
<package name="example" namespace="/example"
extends="struts-default">
<action name="A" class="ExampleAction">
<result name="test">/test.jsp</result>
</action>
</package>
//此处无命名空间
<package name="example2" extends="struts-default">
<action name="B" class="ExampleAction">
<result name="test">/test.jsp</result>
</action>
</package>
三、客户段建立 test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%
System.out.println(request.getRemoteAddr());
System.out.println(request.getRemoteHost());
System.out.println(request.getServletPath());
System.out.println(request.getRemotePort());
System.out.println(request.getServerPort());
System.out.println(request.getContextPath());
StringbasePath=request.getScheme()+"://"+request.getRemoteAddr()+":"+request.getRemotePort()+request.getContextPath();
%>
<title>form多字段,多form</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>
<!-- 这个是没有命名空间的 -->
<s:form action="B.action" >
用户姓名:<s:textfield name="userName" id="userName"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
<!-- 这个是有命名空间的 -->
<s:form action="A" namespace="/example">
用户姓名:<s:textfield name="userName" id="userName"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
<!-- 这个普通form提交有命名空间的 -->
<form action="<%=basePath%>A.action" method="post" >
用户姓名:<input name="userName">
<input type="submit" value="tijiao">
</form>
<!-- 这个普通form提交无命名空间的 -->
<form action="B.action" method="post">
用户Id:<input name="userId"/>
用户姓名:<input name="userName">
<input type="submit" value="tijiao">
</form>
<s:property value="userName"/>
<br>
</body>
</html>
看到了吧,如果用struts标签,且struts配置有命名空间,则<s:form>中也要注明namespace,并且提交的action类中无需家action后缀
如果用struts标签,且struts配置中无命名空间,则<S:FORM>无需注明namespace,提交的时候加上action类名和action后缀即可
如果是html方式的form提交,且struts配置中有命名空间,则action=“”中要注明提交路径和action的名字。如我的 机子是127.0.0.1且http协议,端口8080,action类名为A,项目名为test,则提交为:http://172.0.0.1:8080/test/A.action
如果html方式的form提交,且struts没有配置没有命名空间,则直接提交action名字即可
转自:http://blog.sina.com.cn/s/blog_75b0b3870101802z.html