我剛開始學習一些Struts2並嘗試創建簡單的網頁。我做了一個Action
類,添加了一些註釋,他們我在web.xml
文件中做了一些JSP並設置了過濾器,但是當我嘗試獲取頁面時,我只看到了JSP的工作,沒有任何Struts2。Struts2沒有使用帶註釋的動作
ClientListAction.java:
@Namespace("/")
@ResultPath(value = "/")
@Result(name = "success", location = "pages/clientList.jsp")
public class ClientListAction extends ActionSupport {
private String username = "TEST";
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
clientList.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Test page Struts 2</title>
</head>
<body>
<h4>Hello <s:property value="username"/></h4>
</body>
</html>
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Clients 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>
</web-app>
網址:
http://localhost:8080/pages/clientList.jsp
結果:
Hello
URL有誤。您正在直接訪問jsp。您應該訪問該操作。 您的網址應該是這樣的:'localhost:8080 \ myapp \ clientListAction' –
我試過了,但它沒有幫助 – Andrew