这个问题的造成原因是:struts.xml配置文件中的package标签的namespace属性'/b'和在网址栏输入'http://shna4w8081nqyz5:8080/Struts2/a/toform.action'的不同,正确的输入是'http://shna4w8081nqyz5:8080/Struts2/b/toform.action'这样。
在struts.xml文件中的配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="a" namespace="/b" extends="struts-default">
<action name="checked" class="com.action.TextAction" method="text">
<result name="success">/index.jsp</result>
</action>
<action name="toform">
<result>/index01.jsp</result>
</action>
</package>
</struts>
在index.jsp页面中
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
<form action="a/logo.action" method="post">
<table>
<tr>
<td>账户:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td><input type="submit" value="登录"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
<a href="a/shiwu.action">error.jsp</a>
</body>
</html>
在index01.jsp页面中
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
<%
List<String> country=new ArrayList<String>();
country.add("美国");
country.add("中国");
country.add("法国");
country.add("英国");
country.add("俄罗斯");
request.setAttribute("countrys", country);
%>
<s:form action="checked" method="post">
<s:textfield name="name"></s:textfield>
<s:password name="password"></s:password>
<s:radio name="country1" list="#request.countrys"></s:radio>
<s:select name="country2" list="#request.countrys"></s:select>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
在TextAction类中
package com.action;
public class TextAction {
String name;
String password;
String country1;
String country2;
public String text(){
System.out.println("已用text()方法");
System.out.println(name);
System.out.println(password);
System.out.println(country1);
System.out.println(country2);
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCountry1() {
return country1;
}
public void setCountry1(String country1) {
this.country1 = country1;
}
public String getCountry2() {
return country2;
}
public void setCountry2(String country2) {
this.country2 = country2;
}
}
在package com.action;
public class TextAction {
String name;
String password;
String country1;
String country2;
public String text(){
System.out.println("已用text()方法");
System.out.println(name);
System.out.println(password);
System.out.println(country1);
System.out.println(country2);
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCountry1() {
return country1;
}
public void setCountry1(String country1) {
this.country1 = country1;
}
public String getCountry2() {
return country2;
}
public void setCountry2(String country2) {
this.country2 = country2;
}
}
在TestAction类中:package com.action;
public class TestAction {
String name;
String password;
public String execute(){
System.out.println("接收到请求");
System.out.println("name:"+name+",password:"+password);
return "success";
}
public String test(){
System.out.println("发送错误的请求");
return "error";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}