Wednesday, 4 December 2013

Call Struts2 action from java script

Action Redirection in java script confirm box

Suppose if you have a requirement to call the struts2 action request from the java script so you are come to right place.

Here i am showing you a demo example to call struts2 action request from java script.

It is very simple example to understand .

(Please note : This example has been developed in struts2.0 version.)

There are the following steps to create the demo example:

1. You have to do a FilterDispatcher entry in your web.xml file which will dispatch your request to struts.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 <filter>
 <filter-name>struts2</filter-name>
 <filter-class>
 org.apache.struts2.dispatcher.FilterDispatcher
 </filter-class>
 </filter>
 <filter-mapping>
 <filter-name>struts2</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 <welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
</web-app>

 2. Now you have to create a struts.xml file .

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
 <constant name="struts.devMode" value="true" />
 <package name="struts2" extends="struts-default" namespace="/">
 <action name="firstCallAction" method="firstCall">
 <result>/firstCallAction.jsp</result>
 </action>
 <action name="secondCallAction" method="secondCall">
 <result>/secondCallAction.jsp</result>
 </action>
 </package>
</struts>

3. Now Create the JSP file :
firstCallAction.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>Aoi Blog Example</title>
 </head>
 <body>
 <h2>First Call Action</h2>
 </body>
</html>

 secondCallAction.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>Aoi Blog Example</title>
 </head>
 <body>
 <h2>Second Call Action</h2>
 </body>
</html>

 confirmBox.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <title>AOI Blog Example</title>
 <SCRIPT type="text/javascript">
 function myconfirmBox(){
 var where_to= confirm("Do you really want to go to this page??");
 if (where_to== true)
 {
 window.location="firstCallAction.action";
 }
 else
 {
 window.location="secondCallAction.action";
 }
 }
 </SCRIPT>
 </head>
 <body>
 <s:submit value="SUBMIT" onclick="myconfirmBox();" align="left"></s:submit>
 </body>
</html>

 Now just you have to create a action class :

public class FirstCallAction extends ActionSupport {
 
 public String firstCall() {
 return SUCCESS;
 }
 public String secondCall() {
 return SUCCESS;
 }
}
 
Now example is ready . if you have any problem regarding to it so please free feel to ask.

No comments:

Post a Comment