January 28, 2015

Java Spring MVC hello World Example in Eclipse


Java Spring Model View Controller Hello World Example in Eclipse


Description : In this post we are sharing only source code for java spring application, For theoretical note you can refer this site I could nt provide a better description on java spring than this. Please note the deployment descriptor (web.xml) where we had provide the name index.jsp as our welcome file.

index page (index.jsp)

<html>
<head>
 <title>Office Management System</title>
</head>
<body>
<a href="hello.html">Click here</a>
</body>
</html>

Deployment Descriptor (web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>CfedOMS</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
 <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>


spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       <context:component-scan base-package="net.cfed.oms.controller" />
   <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

JSTL (Java Standard Tag Library) : Expression Language ( EL) makes it possible to easy access application data stored in java bean.
Format : ${expression}

hello.jsp

<html>
<head> <title> This is my First application in Java Spring MVC </title></head>
<body>  ${message} </body>
</html>

What and Why annotation in java ?
Annotation allows implemented class to be auto-detected during class path spanning.
format : @component

RequestMapping is most used annotation method in java spring. It is used to map web request to a specific handler method. Here ModelAndViewHelloWorld Method. This concept is similar to java jsp servlets

Controller it indicates that the annotated class is a controller i e a web controller. 

HelloworldController.java

package net.cfed.oms.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView helloWorld() {
        String message = "Hello, This is my first sample spring MVC project !";
        System.out.println(message);
        return new ModelAndView("hello", "message", message);
    }

}


Project structure


Why should i choose java spring ?
Once you configured java spring, you can easily work on the business logic of your application.

http://javabelazy.blogspot.in/

January 11, 2015

How to create Dynamic table in java swing

How to create Dynamic Jtable in java swing using DefaultTableModel


Description : The program helps you to create a table in java swing, a new row will be automatically inserted into jtable, when the focus reaches on the end of the last row


/** DyanmicJTable
*
* version 1.0
** java source code download
+vinu vp
*/

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.Font;
import java.awt.Rectangle;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import java.awt.GridBagConstraints;

/**
*
*/
/**
* @author MH370
*
*/
public class DyanmicJTable extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton sendBtn = null;
private JTextField firmCodeTxt = null;
private JPanel jPanel = null;
private JScrollPane jScrollPane = null;
private JTable priceTbl = null;
private DefaultTableModel tableModel = null;
/**
* This is the default constructor
*/
public DyanmicJTable() {
    super();
    initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
    this.setSize(600, 600);
    this.setContentPane(getJContentPane());
    this.setTitle("Price Indent Sender");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
    if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.add(getJButton(), null);
            jContentPane.add(getJTextField(), null);
            jContentPane.add(getJPanel(), null);
        }
    return jContentPane;
    }
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (sendBtn == null) {
sendBtn = new JButton();
sendBtn.setBounds(new Rectangle(218, 50, 176, 23));
sendBtn.setText("send file");
sendBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {

 
System.out.println(" sending file ..."); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return sendBtn;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
    if (firmCodeTxt == null) {
        firmCodeTxt = new JTextField();
        firmCodeTxt.setBounds(new Rectangle(30, 49, 170, 26));
    }
    return firmCodeTxt;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
    if (jPanel == null) {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.gridx = 0;

        jPanel = new JPanel();
        jPanel.setLayout(new GridBagLayout());
        jPanel.setBounds(new Rectangle(22, 105, 524, 214));
        jPanel.add(getJScrollPane(), gridBagConstraints);
    }
return jPanel;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
        if (jScrollPane == null) {
                jScrollPane = new JScrollPane();
                jScrollPane.setViewportView(getJTable());
            }
    return jScrollPane;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
    private JTable getJTable() {
        if (priceTbl == null) {
            tableModel = new DefaultTableModel();
            tableModel.addColumn("Item code");
            tableModel.addColumn(" M R P");
            tableModel.addColumn(" Price");
            tableModel.addColumn(" Autobatch ");
            // Initial row
            String [] data = {"","","",""};
            tableModel.addRow(data);
           
            priceTbl = new JTable(tableModel);
            Font font = new Font("Book Antiqua", Font.PLAIN, 20);
            priceTbl.setRowHeight(25);
            priceTbl.setFont(font);
            priceTbl.getModel().addTableModelListener(new TableModelListener() {
               
                @Override
                public void tableChanged(TableModelEvent e) {
                    // TODO Auto-generated method stub

                    if(e.getColumn()==3 && (tableModel.getRowCount()-1)==e.getLastRow()){
                       
                       
                        String [] newRow = {"","","",""};
                        tableModel.addRow(newRow);
                       
                    }
                   
                }
            });
           

        }
        return priceTbl;
    }
   
   
public static void main(String[] args) {
    DyanmicJTable ebola = new DyanmicJTable();
    ebola.setVisible(true);
    ebola.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

Java swing dynamic table example

how to create dynamic table in java http://javabelazy.blogspot.in/

January 06, 2015

How to create a Magic square in java

Java code to print Odd Magic Square Matrix

The code is working fine for only 3 x 3 matrix.


/**
 * @author micromax
 *
 */
public class MagicSquare {
   
    private int [][]magicSquare = null;
   
    private void computerMagicSquare(int matrixOrder) {
        // TODO Auto-generated method stub
        int initialValue = (matrixOrder/2);
        int row = initialValue;
        int col = initialValue;
        int loopCount = matrixOrder * matrixOrder;
        int maxMatrixSize = matrixOrder-1;
        magicSquare = new int[matrixOrder][matrixOrder];
       
       
        for(int value=1; value <=loopCount; value++){ // loop value from 1 to square(order)
            //System.out.println(" value :"+value +" Row :"+row+" Col :"+col);
            magicSquare[row][col] = value;
            row = row + 1;
            col = col - 1;
            if(col<0){ col=maxMatrixSize;}
            if(row>maxMatrixSize){ row=0;}
            if(magicSquare[row][col]>0){
                row=row-1;
                col=col+2;
                if(col>maxMatrixSize)
                    col=0;
                if(row<0)
                    row=maxMatrixSize;
            }
        }
}
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MagicSquare magicSquare = new MagicSquare();
        //System.out.println(" Enter the order of matrix ");
       
        //Scanner scanner = new Scanner(System.in);
        //int matrixOrder = scanner.nextInt();
        int matrixOrder = 3;
        magicSquare.computerMagicSquare(matrixOrder);
        magicSquare.dispalyMagicSquare(matrixOrder);

    }

    private void dispalyMagicSquare(int matrixOrder) {
        // TODO Auto-generated method stub
        System.out.println("Displaying Magic Matrix ");
        for(int row=0; row<matrixOrder; row++){
            for(int col=0; col<matrixOrder; col++){
                System.out.print(magicSquare[row][col]+"\t");
            }
            System.out.println("\n");
        }
       
    }



}

  


 Output:





Author +belazy



 http://javabelazy.blogspot.in/

Facebook comments