DateTimeFormater Class usefull while working with dates in java

This Class will become very usefull when you are dealing with Dates in Java. This class provides method which you can call based on your needs.
 

File Name : DateTimeFormater.java

posted under , | 0 Comments

Spring Exception Handling Example

Spring Exception Handling Example

The following example show how to write a simple web based application using Spring MVC Framwork, which can handle one or more exceptions raised inside its controllers. To start with it, let us have working Eclipse IDE in place and follow the following steps to develope a Dynamic Form based Web Application using Spring Web Framework:

Step 1: Create a Dynamic Web Project with a name SpringException and create following 3 new folder under src folder in the created project
1. Controller
2. Service
3. Dao

Step 2: Copy Spring and other project specific libraries into the folder WebContent/WEB-INF/lib

Step 3 : Now create below packages in specific folder 
1. In Controller folder create package : com.gea.controller  
2. In Service folder create package : com.gea.services  
3. In Dao folder create package : com.gea.dao  

Step 4:  Create Spring configuration files Web.xml and spring-servlet.xml under the WebContent/WEB-INF folder.
 
Step 5: Create a sub-folder with a name jsp under the WebContent/WEB-INF folder. Create a view files 404error.jsp,errro.jsp,ExceptionPage.jsp,index.jsp,viewForm.jsp.

Step 6: Now Follow below steps and run program in your browser.

WelcomeController.java


SpringException.java
WelcomeServices.java

posted under , | 0 Comments

Spring MVC Fast Tutorial: Hello World

How Spring MVC works

Basically the same way as Struts: 

 

  •  Based on the HTTP request URL, the DispatcherServlet calls the corresponding Controller.
  • A View is rendered and sent as HTTP response.

Spring Servlet Declaration

In 'WEB-INF/web.xml', we declare Spring DispatcherServlet and map '*.html' URLs to it:

 
  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    1
  
 
  
    springmvc
    *.html
  
 
  
    
      jsp/index.jsp
    
  
 

Spring Servlet Configuration

Let's now create the Spring configuration file 'WEB-INF/springmvc-servlet.xml' (name based on the servlet name above):

 
  
 
  
      
      
      
  
 

Map URL /hello_world.html to Controller HelloWorldController Declare View Resolver: when view 'view_name' is called (from the Controller), the file '/jsp/view_name.jsp' will be used.

Controller

Let's create the Controller 'WEB-INF/src/springmvc/web/HelloWorldController.java':
package springmvc.web;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
 
public class HelloWorldController implements Controller {
 
 public ModelAndView handleRequest(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
 
  String aMessage = "Hello World MVC!";
 
  ModelAndView modelAndView = new ModelAndView("hello_world");
  modelAndView.addObject("message", aMessage);
 
  return modelAndView;
 }
}
This Controller calls the view 'hello_world', passing 'message' to it (like a Struts attribute).

View

Displays the message attribute previously set in the Controller.

Make it work

 

 

http://localhost:8180/springmvc/hello_world.html

  • We declared Spring servlet in web.xml 
  • We created a Spring configuration file where we mapped an URL to a controller and defined a way to resolve views. 
  • We created a controller and a view

posted under , | 0 Comments

How to Creating Web fragment projects ?

Steps by Step guide for creating Web fragment projects / web module or module project in java.

You can use the Web fragment project wizard to create Web fragment projects in your workspace.

A web fragment is a logical partitioning of the web application in such a way that the frameworks being used within the web application can define all the artifacts without requiring you to edit or add information in the web.xml. It can include almost all the same elements that the web.xml descriptor uses, with these requirements:

  • The top level element for the descriptor must be web-fragment
  • The corresponding descriptor file must be called web-fragment.xml
If a framework is packaged as a jar file and has metadata information in the form of deployment descriptor, then the web-fragment.xml descriptor must be in the META-INF/ directory of the jar file. 

A web fragment is a mechanism for either defining or extending the deployment descriptor of a web application by means of pluggable library jars that contain both the incremental deployment information (in the web-fragment.xml) and potentially any related or relevant classes. The web fragment is also packaged as a library (jar), with the web-fragment.xml in the META-INF directory. Consequently, the web fragment project is essentially a Utility project, with the addition of a web fragment facet to it. The web fragment facet enables you to add relevant context-sensitive functionality to the fragment project.

The stage needs to be set just so.
  1. In the Java EE perspective, select File > New > Web Fragment Project. Alternatively, right-click the Enterprise Explorer context menu, and select New > Web Fragment Project . The Web fragment wizard opens.
  2. In the Name field, type a name for the Web fragment project. To change the default Project contents, click the Browse button to select a new location. If you specify a non-default project location that is already being used by another project, the project creation fails.
  3. The Target runtime field is pre-populated with the selection from the enterprise project.
  4. Optional: Select a pre-defined project configuration from the Configurations drop-down list.
  5. Optional: If you want to modify the configuration details, click modify:
  6. Optional: Select one or more project facets from the Project Facets list. To specify server runtime environments, click Show Runtimes and select one or more runtimes. After making your selections, you can save your custom configuration by clicking Save.
  7. Optional: Select the Add project to Dynamic Web project check box to add the new module to an enterprise module (WAR) project. Type a new project name or select an existing enterprise module project from the drop-down list in the Dynamic Web project name combination box. Or, click New to launch the New EAR module Project wizard.
  8. Select Add project to working sets to add the Web fragment project to an existing working set, or click Select to locate a working set, and click Next.
  9. On the Configure project for building a Java application page, on the Source folders on build path field, click Add Folder... to add folders for your source on the build path, or accept the default value (src).
  10. In the Default output Folder: field, specify a folder for your output files or accept the default value (bin), and click Finish.

How to escaping comma and double quote for CSV file?

We have a solution to escape comma and double quoted characters for CSV file

There are several utilities to escape text for inclusion in a CSV file. One is Apache Commons Lang.

Apache Commons Lang includes a special class to escape a variety of other chains, according to need, StringEscapeUtils.

If you mention you could use something like the following:
It is also possible to invert the way for recover the original string:

posted under | 4 Comments
Newer Posts Older Posts Home

Followers

Powered by Blogger.

Populares

Custom Search