1. 先上代码
代码结构图如下
2. 展示代码
- WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Struts2Test</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- src/struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 设置开发模式,每次修改代码保存后会重新加载国际化资源文件和配置文件 -->
<constant name="struts.devMode" value="true"></constant>
<package name="HelloWorldPkg" namespace="/" extends="struts-default">
<action name="HelloWorldAction_*" class="com.demo.struts2.action.HelloWorld" method="{1}">
<result name="success">/index.jsp</result>
<result name="add">/add.jsp</result>
<result name="save">/index.jsp</result>
<result name="update">/add.jsp</result>
<result name="edit">/add.jsp</result>
</action>
</package>
</struts>
- src/com/demo/struts2/action/HelloWorld.java
package com.demo.struts2.action;
import java.io.IOException;
import org.apache.struts2.ServletActionContext;
public class HelloWorld {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String execute() {
System.out.println("execute");
return "success";
}
public String add() {
System.out.println("add");
return "add";
}
public String save() {
System.out.println("save");
try {
ServletActionContext.getResponse().getWriter().println("Save successful");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public String edit() {
System.out.println("edit");
return "edit";
}
public String update() {
System.out.println("update");
try {
ServletActionContext.getResponse().getWriter().println("Update successful");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
- /WebContent/add.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="HelloworldAction_save" namespace="/" method="post">
<s:textfield label="name" name="name"></s:textfield>
<s:textfield label="age" name="age"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
2. 当我访问 http://localhost:9090/Struts2Test/HelloWorldAction_add.action(我的端口是9090,我自己设置的)时,出现如下图的错误
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
- There is no Action mapped for namespace [/] and action name [helloWorldAction_add] associated with context path [/Struts2Test].
查看 Referenced Libraries下的struts2-core-2.5.16.jar 包下的 org.apache.struts2.default.properties文件发现struts2.5 默认是关闭动态方法调用的,于是就在struts.xml中设置这个常量为true,具体看图(注意图中红色框的部分)
3. 当我再次访问该网址时还是出现错误
阅了很多资料,看到有人说,现在struts2.5 会执行严格方法调用所以需要在struts.xml的action中指明会有哪些方法。在action中result后使用 <allowed-methods> 指定有哪些方法是匹配的
4. 如果你这样设置了还是出错那就只有一个可能你的路径不正确。
注意: action 的 name 是大小写敏感的。eg: 我这里面action的name为HelloWorldAction_* ,那么访问链接就必须要是
http://localhost:9090/Struts2Test/HelloWorldAction_add.action 正确
http://localhost:9090/Struts2Test/HelloworldAction_add.action 错误
http://localhost:9090/Struts2Test/helloWorldAction_add.action 错误
这个是严格区分大小写的。这个是严格区分大小写的。这个是严格区分大小写的。重要是事情说三遍