Spring MVC and Spring Webflow basic and simple explanation

Spring MVC basic and simple explanation

1. As a pre-requisite, org.springframework libraires are to be downloaded and added in the project.

2. All mappings are declared in the Controller Class, not in any xml files.

3. @RequestMapping annotation maps the specific URI as accessed by the the browser.

Ex. Map the URI “/welcome” and when the complete URI http://localhost:8080/spring2/welcome
is accessed by the browser, the Controller Class with “welcome” @RequestMapping is accessed.

4. A specific method will return the ModelAndView Class, where the jsp file name is
the String constructor initialization of the ModelAndView class.

Ex. ModelAndView model = new ModelAndView(“HelloWorldPage”);
where HelloWorldPage is the name of the jsp file

5. All other general configurations are set in spring-mvc-config.xml and web.xml files.

See a more comprehensive tutorial here:

http://www.mkyong.com/spring-mvc/spring-mvc-hello-world-annotation-example/

 

Spring Web MVC and Spring Webflow combined.

Spring Web MVC is the url->to-> controller mapping, while Spring Webflow is the transition controller of a page to another page using a flow configuration.

If both frameworks are combined, the Spring Web MVC will return the initial page, then in this initial page, an option to transfer to another page is processed by the Spring Webflow using xml configuration, then the Spring libraries takes care of the rest….

here’s a neat post where Spring Web MVC and Spring Webflow is combined, for comparison.

https://tamasrathonyi.wordpress.com/2010/09/22/spring-web-mvc-with-spring-webflow/

Webpage development in Java using Spring MVC, JSTL, Javascript and JQuery

How this website is structured in development:

1.) VIEW format – This is the front page view as shown in the browser. components: HTML and CSS for page layout, Spring MVC tags and JSTL for JSP for Java integration to Controller side.

2.) VIEW logic – Javascript and Jquery is used to create button/checkbox logic and is integrated to the HTML

3.) CONTROLLER – Built using Java classes using J2EE/HTTP libraries with Servlet Request and Response parameters. Spring annotations are also included to bind the Controller and the Models

4.) MODEL – Java classes that are POJOs ( Plain-Old-Java-Objects ) which has fields, setters and getters. Models are data storages on the transaction level. These are integrated to the JSP page and can also be used to be integrated to the Database frameworks such as Hibernate and Spring with Hibernate.

Before JSTL, the Java, Spring, Hibernate and Struts design was popular. Struts was used to bind the UI components to the Java and Spring server side code. Here is my post on Struts, Spring, and Hibernate configuration.

Difference between EAR and WAR files

Difference between EAR and WAR files

*.war file – this file is a composition of servlet class files. This package is HTTP request/response capable, can be accessed by a URL and can return a page. The contents are Java Classes, JSP files, GIF ( for website images ), and HTML files. Can be deployed on a Tomcat or Jetty servlet container.

*.ear file – this file is the composition of a enterprise application, that is composed of several war and ear files that functions as a single application. ear files are usually complete with HTTP request/response capability, Database access, Exposed API’s and other fucntionalities. EAR files are deployed on application servers such as Weblogic, JBoss or IBM Websphere. If you deploy a war file on application server, it is wrapped to an ear file, for standard deployment.

*.jar file – is the basic java archive file. A compilation of Java Classes that functions as a single application. May include Java Classes only.

Here are some more detailed answers on stackoverflow.com
http://stackoverflow.com/questions/1594667/war-vs-ear-file

Google Calendar API – sample codes

I’m continuing my Google Calendar API, and here are some sample codes. I added a table in HTML to display the retrieved calendar values. See the sample code below and the screenshots.

NOTE: in creating Java code in JSP, Java codes are executed first, then all values will be added in HTML transactions and scripts after, you just can’t make values in a JavaScript to change in a Java loop, and vise-versa. they do not run concurrently.

 


<%
CalendarData cData = new CalendarData();
String testValue = cData.getDataFromStartServlet();

int sample = 0;
List <CalendarDataBean> CDataBeanList = cData.getDatabean();

String finalValue = "no value";

CalendarDataBean displayCalendarData =  cData.getDatabean().get(sample);

StringBuilder builder = new StringBuilder();
for(CalendarDataBean b: CDataBeanList){
    builder.append("<tr>");
    builder.append("<td>Event:" + b.getEventName() + "</td>");
    builder.append("<td>Start Time:" + b.getStartTime() + "</td>");
    builder.append("<td>End Time:" + b.getEndTime() + "</td>");
    builder.append("<tr>");
}

finalValue = builder.toString();
%>

event_in_table_form event_in_table_form_calendar

 

web.xml explained

The web.xml file is a necessary component of a Java Web Application. This file is located under the WEB-INF folder. This file is used to map/match the URL to a servlet or a page. Just note that the value in <servlet-name> tag matches in <servlet> tag and <servlet-mapping> tag. <servlet-class> would be the Java class, and <url-pattern> would be the URL.

Here’s an example for URL

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>file</servlet-name>
        <servlet-class>mysite.server.FileServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>file</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

Here’s an example for a page, in this case a jsp file.

(/*) means any other URL , In this example it would be any other URL after /login

    <servlet>
        <servlet-name>login</servlet-name>
        <jsp-file>/register/login.jsp</jsp-file>
    </servlet>

    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/login/*</url-pattern>
    </servlet-mapping>

we can also initialize servlet parameters in web.xml. Here’s an example below:

    <servlet>
        <servlet-name>example</servlet-name>
        <servlet-class>mysite.server.Location</servlet-class>
        <init-param>
            <param-name>location</param-name>
            <param-value>cebu</param-value>
        </init-param>
    </servlet>


For more information on web.xml, here’s a good tutorial.

https://developers.google.com/appengine/docs/java/config/webxml

Google Calendar API

I made a mini project before that can retrieve Google Calendar events and Data. The API is publicly provided by Google. Please see the screenshots I made. It was a Web application project deployed in Tomcat. It retrieves Google Calendar data from an account and displays the result in a website an in a Google compose mail window also.

calendar
The Google Calendar data. Notice the “Outdoor Hike” schedule, And how the other data is retrieved

 

webpage
(Simple Text) Calendar data displayed in a website
compose_mail
(Simple Text) Calendar data displayed in the Google compose mail page
tomcat_deployment
the project files

 

 

How to create JSP response without hardcoding the HTML in java code

HTML lines can be hard-coded in the Java Class, but… this technique is not advised. In the MVC Architecture, The View component must be separated from the Controller component. And JSP views must be separate for easy editing, No need to compile the Java Class when there is a modification.

The technique to do this is using the following

1.) set request.setAttribute()this binds the variable to be displayed in the View as set in the Java Controller Class.

2.) In the JSP View, create the ${variable} to accept the input from the Controller Class

3.) To create the response, do request.getRequestDispatcher("/<response>.jsp").forward(request, response);

Where <response> is the JSP file that displays the variables.

It is explained more in this links:

http://stackoverflow.com/questions/6452537/servlet-send-response-to-jsp

http://stackoverflow.com/tags/servlets/info