Question

I just start to learn some Struts2 and try to create simple web page. I made a Action class, add some annotation, them I made some JSP and set filter in web.xml file, but when I try to get page I just see work of JSP not any 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>

URL:

http://localhost:8080/pages/clientList.jsp

Result:

Hello 
Was it helpful?

Solution

Remove @ResultPath(value = "/") and set the absolute path for location attribute. Also in the result annotation name = "success" is used by default, writing it isn't necessary. To use annotation configuration required to add struts2-convention plugin to the module dependencies. To execute action you need to enter url

http://localhost:8080/client-list

if your module is deployed to the root context, or add context path before the action name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top