Uploading files in Struts

Uploading files in Struts
Struts has default interceptor (in-build interceptor) for uploading files, however not that these files will get stored in temporary directory they should be processed and stored in permanent location.  (Refer: Default Interceptors in Struts)

Lets see an example of uploading an image using Struts,

Directory Structure
Structure

We have created 2 jsp files, one dao.java for this example.

upload.jsp

success.jsp

dao.java

struts.xml

Output:
op1
op2
Uploaded Successfully!

Now check server.log to know the location where the image got saved,

Image Location: E:\NetBeans\Struts2FUL\build\web\images

 

op4

 

 

 

 

Read More

execAndWait in Struts Example

execAndWait in Struts Example

execAndWait is one of the default-Interceptor in Struts (you can view default-interceptors here). The execAndWait interceptor shows an intermediate webpage when result page is loading.

Structure of Project:
structure

Create 3 JSP files – login.jsp,loading.jsp,Success.jsp

login.jsp

loading.jsp

Success.jsp

dao.java

Struts.xml

Output:
login
loading
success

 

 

 

 

 

Read More

Custom Interceptor in Struts 2

Custom Interceptor in Struts 2

What are Interceptors?
Interceptors are used for performing operations like Validation, file upload, exception handling etc.

Advantages of Interceptors:
Separation of Concerns, If we need to remove any of the concerns like validation or exception handling etc.. We can remove them from struts.xml without disturbing the application.

Struts 2 provide a set of default interceptors (default interceptors), we can also create and implement our own interceptors. Creating and implementing own interceptors are known as Custom Interceptors.

Lets see an example for custom interceptors, We are going to check the time for the process.

Structure:
struct

Lets create 2 JSP files – input.jsp and showdetails.jsp

input.jsp

showdetails.jsp

login.java

MyInterceptor.java

struts.xml

Output:
input
show
Console output to check the timing function,

[2015-09-18T20:52:01.795+0530] [glassfish 4.0] [INFO] [] [] [tid: _ThreadID=101 _ThreadName=Thread-3] [timeMillis: 1442589721795] [levelValue: 800] [[
Start time is: 1442589721795 ms]]

 

 

 

 

 

Read More

Interceptors in Struts2

Interceptors in Struts2

What are Interceptors?
Interceptors are used for performing operations like Validation, file upload, exception handling etc.

Advantages of Interceptors:
Separation of Concerns, If we need to remove any of the concerns like validation or exception handling etc.. We can remove them from struts.xml without disturbing the application.

Default Interceptors:
Struts 2 has some in-built default interceptors, also we can build our own custom interceptors. In this post, we are going to explore default interceptors.

Interceptor
Description
alias
Parameters to have different name aliases
checkbox
Managing checkboxes to check which is checked and unchecked
conversionError Error message when converting string to parameter value
createsession
creating new session
execAndWait
Sends intermediary waiting page when process executes background
Exception
Throwing exceptions
fileupload
for uploading files
logger
simple logging by displaying name
params
request parameters on the action
Prepare
Pre-preocessing works
timer
Shows how long the it takes for action to execute
token
checks for valid token to prevent duplication
validation
provides validation

Lets see an example,

Lets check the “timer” interceptor to check the time taken for executing the action.

Structure:

structure

first.jsp

display.jsp

Sdao.java

struts.xml

Output:
op1
op2
In your log file you can see the output of timer interceptor,
op3

 


 

 

Read More