Passing object from jsp page to action using model driven

Project Explorer

3_exp
addstudent.jsp

struts.xml

StudentAction.java

Student.java

showstudent.jsp

OUTPUT

3_ip

3_op

Passing parameter from jsp page to action

Project Explorer
firstexp
Library files
firstlib

addstudent.jsp

struts.xml

StudentAction.java

showstudent.jsp

web.xml

Output

firstip

firstop

Getting Ip address of client

When user submit a form along with form data we can get Ip address of client.
Lets see one simple program to demonstrate this

Redirect action to another Url

We want to redirect a action to another website or url.
for that syntax is used

type=”redirect” is used to redirect this action to specified url.
here url is a variable in action class with getter and setter methods.
We are getting url value from jsp page.to get url value in struts.xml we used ${url}.

First.jsp

Struts.xml

RedirectAction.java

Output

redirect action
redirect action

redirect result

html/jsp page response in ajax get method

We want to get an entire page as ajax response by using jquery get method.

Steps:

1) First create pages(FirstJsp.jsp and SecondJsp.jsp)
2) call jquery get method to get ajax response in one page(here FirstJsp.jsp to get SecondJsp.jsp)
3)Configure struts.xml file

Lets start with page creating

FirstJsp.jsp


SecondJsp.jsp

Struts.xml

Output
Ajax1

2nd

Flying saucer with struts2

Flying Saucer takes XML or XHTML and applies CSS 2.1-compliant stylesheets to it, in order to render to PDF (via iText), images, and on-screen using Swing or SWT. The library implements (basically) the entirety of CSS 2.1 and aims to be fully compliant with the W3C specification; it includes a small handful of CSS 3 features.[source]

In jsp page

Setting page theme simple

In particular jsp page we can set that page simple by including following tags
In jsp page

This will set page theme simple.
It will not generate struts2 tabular format user has to create his own format while using theme simple.

Example:

Excel file download in Struts2 using apache POI

Apache POI is project by Apache Software Foundation. It provides the Java API for Microsoft Documents.

You can find more about apache POI . You can also download apache POI from here.

Here we are explaining how to export data from struts2 action to xml file.
For that we are using apache poi.


We have included following jar files in lib folder

In struts.xml

In jsp

Showing Arraylist values using Struts2 Json and JQuery

I am having comment class, I want to show its property values to jsp page using json

Comments.java

ListJson assigning values for comments.java. Here we fetch value of commentList and totalcomments value in our jsp page.

Struts.xml

In Jsp page

Struts2 Json and Jquery Example

Here we will fetch a action variable’s value in jsp page using JSON and jquery
Here I am using struts 2.3.4 for that we have to include struts-2-json-plugin-2.3.4.jar in class path or include it to your lib folder if you are using any IDE.

Action class

In struts.xml we have to extend json-default and result type shold be json. No need to specify path inside <result &rt;</result<

struts.xml

Jsp page

Here we are using $.getJSON(‘test’, { name: name1}, function(data) method to fetch json data.