2
votes

I am migrating a Struts2 application to Spring Boot. I defined the StrutsPrepareAndExecuteFilter inside my Application boot class.

@SpringBootApplication
public class ClearApplication extends SpringBootServletInitializer {

  public static void main(String[] args) {
    SpringApplication.run(ClearApplication.class, args);
  }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(ClearApplication.class);
  }

  @Bean
  public FilterRegistrationBean filterRegistrationBean() {
    Collection<String> urlPatterns = new ArrayList<>();
    urlPatterns.add("/*");
    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    StrutsPrepareAndExecuteFilter struts = new StrutsPrepareAndExecuteFilter();
    registrationBean.setFilter(struts);
    registrationBean.setName("strutsFilter");
    registrationBean.setOrder(1);
    registrationBean.setUrlPatterns(urlPatterns);
    return registrationBean;
  }
}

The pom.xml file configuration is below

<groupId>com.study.pack</groupId>
<artifactId>clear</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>clear</name>
<description>Demo project for Spring Boot</description>

My action class has one simple Struts action and another json action

@Action(value = "welcome", results = { @Result(name = "success", location = "result.jsp") })
public String execute() {
    return "success";
}

@Action(value = "users", results = { @Result(name = "success", type = "json", params = { "root", "userList" }) })
public String users() {
    userList = new ArrayList<>();
    userList.add(new User("Carl", "15", "M"));
    userList.add(new User("Rick", "30", "M"));
    userList.add(new User("Michonne", "26", "F"));
    return "success";
}

When, I am deploying the application on my local Tomcat server. I am able to access the action URLs

http://localhost:8080/clear/welcome

http://localhost:8080/clear/users

But, when I run the application in STS (or by using maven command), I am getting the standard struts error when accessing the same above URLs

There is no Action mapped for namespace [/clear] and action name [users] associated with context path [].

The application.properties has the following properties

server.contextPath=/clear
server.port=8080 

Am I missing any other configuration to get it working ?

Update:

As @Roman-C suggested, I checked the startup logs of tomcat server and the context path is identical.

External Tomcat Server:

[ost-startStop-1] o.a.c.c.C.[.[localhost].[/clear]         : Initializing Spring embedded WebApplicationContext

Embedded Tomcat Server:

[ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/clear]  : Initializing Spring embedded WebApplicationContext

The application.properties lies within the folder

src/main/resources     

Still No luck. Somehow the struts annotations are ignored I guess.

I have committed the source code in GitHub.

1
Bala, have you had any luck on this matter? Thanks. - Luiz Feijão Veronesi
Sadly, I did not. - Bala

1 Answers

2
votes

By default Spring Boot initializes the embedded tomcat to the root application context. So you can access your application from /.

The URL is mapped to the action by the action mapper. The default action mapper's implementation is in DefaultActionMapper that parses the URL and extracts action name and namespace. The path before the last slash after the context path is a namespace, the action name is the remaining path.

The application.properties

server.contextPath=/clear
server.port=8080 

works fine if they are located in src/main/resources using maven's project structure.

Check log messages that should have the following line

INFO 4496 --- [ost-startStop-1] o.a.c.c.C.[.[localhost].[/clear]       : Initializing Spring embedded WebApplicationContext