How to do dynamic URL redirects in Struts 2?

Here’s how we do it: In Struts.xml, have a dynamic result such as: <result name=”redirect” type=”redirect”>${url}</result> In the action: private String url; public String getUrl() { return url; } public String execute() { [other stuff to setup your date] url = “/section/document” + date; return “redirect”; } You can actually use this same technology to … Read more

“The Struts dispatcher cannot be found” error while deploying application on WebLogic 12.1.3

If you are using Struts tags inside JSP page that has listed in the welcome-file-list it should be removed. welcome-file-list in web.xml: The welcome-file-list element of web-app, is used to define a list of welcome files. Its sub element is welcome-file that is used to define the welcome file. A welcome file is the file … Read more

How to make an Action to accept dynamic JSON data from the user interface in Struts 2?

Post them as content with type “application/json”. You may do it with a simple jQuery Ajax call where you could specify the content-type and dataType. $.ajax({ type: “POST”, url: “/the/action/url”, data : {}, dataType:”JSON”, contentType: “application/json; charset=utf-8” }); Add json plugin to the project dependencies with json-lib-2.3-jdk15.jar. The plugin supplied with the interceptor json that … Read more

Spring CSRF token does not work, when the request to be sent is a multipart request

If you are using @annotations, and the jsp view like this: <form:form id=”profileForm” action=”profile?id=${param.id}” method=”POST” modelAttribute=”appUser” enctype=”multipart/form-data” > … <input type=”file” name=”file”> … <input type=”hidden” name=”${_csrf.parameterName}” value=”${_csrf.token}” /> </form:form> this may help: AppConfig.java : @EnableWebMvc @Configuration @Import({ SecurityConfig.class }) public class AppConfig { @Bean(name = “filterMultipartResolver”) public CommonsMultipartResolver filterMultipartResolver() { CommonsMultipartResolver filterMultipartResolver = new CommonsMultipartResolver(); … Read more

java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter when starting Simple Struts2 Application

Use this command to convert Maven project to Dynamic Web Project for Eclipse: mvn eclipse:eclipse -Dwtpversion=3.4.2 you may change whatever WTP version you use .classpath should be like this <classpath> <classpathentry kind=”src” path=”src/main/java” including=”**/*.java”/> <classpathentry kind=”src” path=”src/main/resources” excluding=”**/*.java”/> <classpathentry kind=”output” path=”target/classes”/> <classpathentry kind=”var” path=”M2_REPO/asm/asm/3.3/asm-3.3.jar”/> <classpathentry kind=”var” path=”M2_REPO/asm/asm-commons/3.3/asm-commons-3.3.jar”/> <classpathentry kind=”var” path=”M2_REPO/asm/asm-tree/3.3/asm-tree-3.3.jar”/> <classpathentry kind=”var” path=”M2_REPO/commons-fileupload/commons-fileupload/1.2.2/commons-fileupload-1.2.2.jar” /> <classpathentry … Read more

Exception starting filter struts2 – tried adding JAR’s, but same result

Since you are using Struts 2.3, FilterDispatcher is deprecated. You MUST use StrutsPrepareAndExecuteFilter (or its little brothers). From the official documentation Deprecated. Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilter and StrutsExecuteFilter if needing using the ActionContextCleanUp filter in addition to this one change then this <!– Struts < 2.1.3 –> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> to … Read more

Dependency Injection in Struts2 Accessing Session Scoped Beans

Let’s start from looking what is a Scope.Strategy by looking at the docs. It says Pluggable scoping strategy. Enables users to provide custom implementations of request, session, and wizard scopes. Implement and pass to Container.setScopeStrategy(com.opensymphony.xwork2.inject.Scope.Strategy) Ok, assume I want to implement session scope. Then I need to know the place where I could implement it. … Read more

Migration from Struts 1 to Struts 2

S1 with S2 it will overkill. Both frameworks are complicated, so the maintenance costs increase twice or more time. So, the strategy is to migrate completely to S2. Both frameworks are implemented MVC pattern. Divide the application on three parts that should migrate separately: Model, Controller, and View. The order is not important, but logically … Read more

How to detect submit button clicked in multiple submit buttons scenario in single Action class? [duplicate]

You can define two actions in struts.xml file and use action attribute of <s:submit> tag in order to submit to different actions http://struts.apache.org/docs/submit.html. In JSP: <s:submit value=”Search” action=”searchEmployeeAction”/> <s:submit value=”Add New” action=”addEmployeeAction”/> In struts.xml: <action name=”addEmployeeAction” method=”add” class=”example.EmployeeAction”> <result>/example/add.jsp</result> </action> <action name=”searchEmployeeAction” method=”search” class=”example.EmployeeAction”> <result>/example/search.jsp</result> </action> And in your action create two public String methods … Read more