我刚开始学习一些 Struts2 并尝试创建简单的网页。我创建了一个Action
类,添加了一些注释,我创建了一些 JSP 并在web.xml
文件中设置了过滤器,但是当我尝试获取页面时,我只看到 JSP 的工作而不是任何 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;
}
}
客户端列表.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>
网页.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