Custom URL in Struts2 for each User

In this tutorial we will see how to assign custom URL for each user logged in Struts2. For example like ‘www.gmail.com/rajesh’  for user rajesh and ‘www.gmail.com/kumar’  for user kumar.

I assume that you already have a basic knowledge over struts 2 framework setup and simple Struts2 application. If you are not familiar please refer Struts2 Introduction and HelloWorld application.

To create custom URL for each user first we have to set the below constants in the struts.xml.

The first constant  struts.action.extension  is to disable the default .action extension from the URL pattern. The next two constants  struts.mapper.alwaysSelectFullNamespace  and struts.enable.SlashesInActionNames for transfering the parameter from the URL. The last one struts.patternMatcher is to use regular expression in the action names (For example <action name=”Lost*do”>).

Next is we have to map the request to an action. To do this create an action like below in struts.xml.

In the above code we mapped the string whichever comes next to the application context to the field username. For example if the request is  http://localhost:8080/CustomURL/rajesh  the framework will assign the value ‘rajesh’ to username field and then will proceed like normal struts2 flow. But one important thing is it will not accept slash in that. For example if the request is http://localhost:8080/CustomURL/test/rajesh the framework will not call the above action. Because the request is for inside a package test. See the finalized code of struts.xml below.

struts.xml

In the above code we added one more action with empty name “”. It is for the index page. So when we type simply the url  http://localhost:8080/CustomURL the home.jsp will be displayed. See our home.jsp below.See our welcome.jsp below.

welcome.jsp

See our action class CustomURL.java below

CustomURL.java

That’s all. Simply place all the above codes in respective files and run.

custom_url_demo_1
custom_url_demo_2
For the above example I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you face any issues while execution please post in comments or send email to rajeshmepco@gmail.com

Posted in Struts2 | Tagged | 1 Comment

Apache HTTP Server – An Introduction

Apache is the world’s most popular Web server/HTTP server. We will see the overview of apache HTTP server here.

How to start / stop / restart apache ?

How to run syntax test for configuration file ?

Configuration

Modules

  • Apache is a modular server, only the most basic functionality is included in the core server.
  • Extended features can be loaded into Apache using LoadModule directive ( Only if the server is compiled to use dynamically loaded modules).

Directives

  • Apache directives are a set of rules which define how your server should run.
  • We can change them by editing the httpd.conf and related files to meet your requirements.

Scope of Directives

Posted in Apache | Tagged , | Leave a comment

Dynamic Drop Down List with Struts2 UI tag without AJAX, jQuery

In this tutorial session we will create simple application to load the drop down list options dynamically from the server side. Also we will use only struts 2 UI tag, not ajax or jquery.

In our previous example we used ajax call to load the drop down options from server side via jquery. Here not.

We assume that we already have a very basic knowledge over struts2 setup. If not please refer our Hello World application. To achieve our goal first create a simple jsp file as below.

home.jsp

Then in web.xml divert all requests to struts2 framework as usual. See the web.xml below.

Also please note that we never mentioned the welcome file list in our web.xml. (i.e) Home page. We will handle that in our struts.xml.

See our struts.xml below.

What is the change we made in our struts.xml. We mapped a empty action to a class and if the result is success then we are rendering the page home.jsp to the browser. Why instead of mentioning the home page in web.xml we mentioned in struts.xml? There is a reason behind that. We will discuss it later.

Now see the corresponding action class ReadData.java below.

ReadData.java

That’s all the setup. But we need to know why we rendered the home page via struts.xml? Also the empty action pointed to the method dbStates() is not doing anything. It simply returns a  "success"  string. Then why?

The answer is here. In our home page home.jsp we used struts2 tag for the drop down list. See the code

So if we render the page directly from web.xml it don’t have any values for the drop down list options. We need to run some code before rendering the page. Right? That’s why we diverted the empty action to ReadData.java . See the constructor of the ReadData.java .

So when we point to this class it will automatically creates an arraylist and fills it with the required value. So after this constructor executed the control will go to the dbStates() method. There we simply returns a “success” string. So the control will come back to struts.xml. There we mentioned <result name="success">home.jsp</result> . So it will render the page home.jsp to the browser.

But the difference is while render the page home.jsp. When it comes to the Struts UI tag

it will look for method  getStat() in the action class. Because we mentioned the name as state. So as per struts 2 design pattern it will try to run a method by name getStat() .

As we have that method in our action class it will get executed. Then with the return value as arraylist it will be filled inside the drop down list by the framework and rendered to the browser .

Simply run the code as below http://localhost:8080/DynamicData/ and see the result in action.
struts2UI_dropdown

For the above example I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you face any issues while execution please post in comments or send me email to rajeshmepco@gmail.com

Posted in Struts2 | Tagged , , , , | 3 Comments

Dynamic Drop Down List with Struts 2 and AJAX

In this tutorial session we will create simple application to load the drop down list options dynamically from the server side. Also we will load one drop down list based on the selection of another drop down list.

We use AJAX, JQuery, Struts2 and json methodologies to achieve this. Also we assume the basic setup for struts2 is already ready. (i.e) All necessary jar files placed in right place. If you want more info please refer the Struts 2 Hello World application.

Create a simple html file with the below content.

Create struts.xml file with the below content and place it under class folder root path.

As we are sending and receiving the data as json object we need to enable the json option in the struts.xml file. So implement a custom interceptor and json result type as above. (To do this struts2-json-plugin.jar is required)

See our web.xml file below.

Also create the java class ReadData which contains the business logic with the below code and place under com.rajesh.struts2 package in the class folder.

See the complete structure of the folder below.

dynamic_dd_structure

That’s all. Simply run the program as below and see the result in action.

http://localhost:8080/DynamicData/

dynamic_dd

For the above example I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you face any issues while execution please post in comments or send me email to rajeshmepco@gmail.com

Posted in Struts2 | Tagged , , , | 5 Comments

Struts2 Tutorial Part 2 – HelloWorld application

In this tutorial session we will create simple helloStruts application. To create a simple struts2 application follow the steps below.

I assume we already have a basic knowledge over jsp/servlet workflow.

As per jsp/servlet whenever we create new jsp/servlet application, the control will look for web.xml inside the application context WebContent/WEB-INF folder. Application Context is nothing but the application name referred by the servlet container (ex. Apache tomcat). See the sample web.xml below.

So the first step we need to do is to divert all requests from web.xml to our struts2 config file. The config file for struts 2  is struts.xml which is fixed. (i.e) We cannot rename the file.

How can we divert the request?

We knew that in web.xml we can add filters to url patterns to do pre/post processing. Here we will use that approach to divert the requests. (i.e) We add a filter to all the url patterns and divert to struts 2 config file.  For that add the below code in web.xml.

What we done in the above code is, we added a filter struts2 to all the patterns (*). So that filter will call the class org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.

No more changes required in the web.xml file. See the consolidated web.xml file below.

Next step is we have to create the configuration file struts.xml for all the requests. As discussed the name of the config file is struts.xml which should be in the class folder root path. See the struts.xml below.

Then create a file with above code and save it with name struts.xml and place it under class folder root path. (i.e) WebContent/WEB-INF folder.

Next step we need to do is adding the necessary jar files to the library. Download the struts2 library files and place it under lib folder inside WEB-INF folder. All the jar files are not required for basic application. For most application below listed jars are enough. Later if we require we can add the necessary jars to the library.

struts2-jars

Next step is to create a necessary jsp, html files. Create the below two files namely index.html and welcome.jsp with the below content and place it under WebContent folder.

index.html

 welcome.jsp

That’s all. See the final structure of the folders below.

struts2-structure

Run the project in eclipse by Run as -> Run on server in eclipse or place the folder structure above in the web-apps folder in tomcat. Then type the below code in the browser (if tomcat is running under port 8080).

http://localhost:8080/helloStruts

You will get the below screen.
struts2-home

For the above method I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you have any issues while execution please post in comments or send mail to rajeshmepco@gmail.com.

Posted in Struts2 | Tagged | Leave a comment

Send HTML Form data to struts2 action by Model Driven method

HTML form data can be transferred to Struts2 Action in three ways.

1. Normal Method with simple getters() and setters()
2. Object Backed Method
3. Model Driven Method

We will see the third method now.

Model Driven Method

Another way of transfering the form data directly inside the object is Model Driven approach. This approach is the best among all the three because for this approach no need to specify the field names deeply. We can define the fields as usual.

Create a simple jsp file index.jsp with struts2 form tags as below. Note that we never named the fields deeply like what we did earlier for object backed approach.

So As per the jsp page when the form is getting submitted it will look for an action with name register. See out struts.xml below.

In our Struts.xml we pointed the register action to register() method in the Register class. So when the form is getting submitted the control will go the register() method.  Now see the Register action class below.

In our register action class we made two changes from Object backed method. One is the class implements ModelDriven interface and it’s method getModel(). So while the control look for setter methods it will run the getModel() method. Then it will take care of the data movement from form to object. Finally it will simply return the user object.

How this is achieved is by the framework interface ModelDriven. One important thing here is we have to create the User Object explicitly outside the method. And as usual the User class should have all the setters() and getters() for the required variables. See our User class below.

So the Struts2 framework will execute the getModel() method before executing the actual action method, in our case register(). So while executing the getModel() method it will look for all the submitted data and if it matches with the method return type then  it will call the corresponding setter methods and return as object. Thus all the variables will be assigned to the user object and then returned.

In our action method we just displayed the values in the console, then returning success message as result name. So as we mentioned the result page is success.jsp for string “SUCCESS” struts2 will load the page success.jsp to the user. See our success.jsp below.

What will happen for those tags like <s:property value ="fname"/> . Struts2 will look for getter() methods in the corresponding action class. As we discussed earlier due to ModelDriven interface implementation both the getter and setter process will be take care by the framework.

So it will run the getModel method and runs the getter methods inside the User object then it will return the object with all values. Then all the fields will be filled then returned to the user as a rendered page.

For the above method I have created a complete .war file with source code. Download and just place under Apache web apps folder and run.

If you have any issues while execution please post in comments or send mail to rajeshmepco@gmail.com.

Posted in Struts2 | Tagged , | Leave a comment
hello