虽然这是一个常见的问题,但我无法弄清楚。
我正在使用maven项目运行Strut2。我的项目结构如下所示
Product.java
package beans;
public class Product {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
ProductAction.java
package actions;
import beans.Product;
public class ProductAction{
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
private Product product;
public String execute(){
return "success";
}
}
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>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="product" class="actions.ProductAction" method="execute">
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>
的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.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>
的index.jsp
<html>
<body>
<h2>Hello World!</h2>
<div>
<%@ taglib uri="/struts-tags" prefix="s" %>
<s:form action="product">
<s:textfield name="product.id" label="Product Id"></s:textfield>
<s:textfield name="product.name" label="Product Name"></s:textfield>
<s:textfield name="product.price" label="Product Price"></s:textfield>
<s:submit value="save"></s:submit>
</s:form>
</div>
</body>
</html>
和 welcome.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<%@ taglib uri="/struts-tags" prefix="s" %>
Product Id:<s:property value="id"/><br/>
Product Name:<s:property value="name"/><br/>
Product Price:<s:property value="price"/><br/>
</body>
</html>
我正在使用Glassfish服务器。它运行index.jsp很好。但是当我填写表格并提交它时显示
HTTP状态404 - 没有映射名称空间[/]和 与上下文路径[/ deploytest]相关联的操作名称[product]。
输入状态报告
message没有映射名称空间[/]和操作名称的Action [product]与上下文路径[/ deploytest]相关联。
description请求的资源不可用。
GlassFish Server Open Source Edition 4.1.1
我的 pom.xml 具有依赖性
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.28</version>
</dependency>