Archive for the ‘ glassfish server ’ Category

Simple Spring MVC Application (Annotation)



General steps taken in the video above.(Not the exact steps)

  1. Clone project from previous blog post: Simple Spring MVC Application. Rename the project to spring-mvc-2
  1. Create AppConfig.java class in package org.greenhorn.spring.config with the below content:
  2. import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    
    @Configuration
    @ComponentScan(basePackages = {"org.greenhorn"})
    public class AppConfig {
    
    	@Bean
    	public InternalResourceViewResolver getInternalResourceViewResolver() {
    		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    		resolver.setPrefix("/WEB-INF/pages/");
    		resolver.setSuffix(".jsp");
    		return resolver;
    	}
    }
    
  1. Delete springmvc-servlet.xml. This has been replaced by AppConfig.java class:
  1. Update web.xml with the spring configuration class, AppConfig:
  2. <init-param>
    	<param-name>contextClass</param-name>
    	<param-value>
    		org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    	</param-value>
    </init-param>
    
    <init-param>
    	<param-name>contextConfigLocation</param-name>
    	<param-value>
    		org.greenhorn.spring.config.AppConfig
    	</param-value>
    </init-param>
    
    
  1. Change HelloReaderController.java. Change RequestMapping to “/hi” and change the “message
  1. Run the project from netbeans.


NOTE
The sample maven project used for this exercise can be downloaded from:

spring-mvc-2

Simple Spring MVC Application



General steps taken in the video above.(Not the exact steps)

  1. Create new maven java web application.
  1. Add spring dependency to pom.xml file:
  2. <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-webmvc</artifactId>
    	<version>4.2.5.RELEASE</version>
    </dependency>
    
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-core</artifactId>
    	<version>4.2.5.RELEASE</version>
    </dependency>
    
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-beans</artifactId>
    	<version>4.2.5.RELEASE</version>
    </dependency>
    
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-context</artifactId>
    	<version>4.2.5.RELEASE</version>
    </dependency>
    
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-web</artifactId>
    	<version>4.2.5.RELEASE</version>
    </dependency>
    
  1. Create web.xml.
  1. Define the Dispatcher Servlet in the web.xml
  2. <servlet>
    	<servlet-name>springmvc</servlet-name>
    	<servlet-class>
    		org.springframework.web.servlet.DispatcherServlet
    	</servlet-class>
    	<load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
    	<servlet-name>springmvc</servlet-name>
    	<url-pattern>*.mvc</url-pattern>
    </servlet-mapping>
    
  1. Create the application context configuration(springmvc-servlet.xml). Select “context” during creation wizard steps. :
  2. <context:component-scan base-package="org.greenhorn" />
    
    <context:annotation-config />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF/pages/" />
    	<property name="suffix" value=".jsp" />
    </bean>
    
  1. Create simple controller HelloReaderController.java in package org.greenhorn:
  2. import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class HelloReaderController {
    
    	@RequestMapping(value = "/hello")
    	public ModelAndView sayHello() {
    		ModelAndView mv = new ModelAndView();
    		mv.addObject("message", "Hello Reader!");
    		mv.setViewName("helloReader");
    		return mv;
    	}
    }
    
  1. Create the JSP file named helloReader.jsp under the /WEB-INF/pages/ folder:
  2. <html>
    	<body>
    		${message}
    	</body>
    </html>
    
    
  1. Run the project from netbeans.


NOTE
The sample maven project used for this exercise can be downloaded from:

spring-mvc-1

Simple jsf with jpa

Create database and table in myql using netbeans (part 1)

Part 1

Creating datasource in Glassfish. Generating jpa in Netbeans. (part 2)

Part 2

jsf using jpa (part 3)

Part 3

The source code of Manager java class used in the video(Part 3) above:

      public class Manager {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory(“florist”);
        EntityManager em = emf.createEntityManager();
        EntityTransaction entityTransaction = em.getTransaction();

        public List getAllUsers() {
            TypedQuery query = em.createNamedQuery(“User.findAll”, User.class);
            List results = query.getResultList();
            return results;
        }
      }

The source code of Bean java class used in the video(Part 3) above:

      public class Bean {
        private Manager mgr;

        public Bean() {
            mgr = new Manager();
        }

        public String getUsers() {
            List users = mgr.getAllUsers();

            String userNames = “”;
            for (User user : users) {
                 userNames += user.getId()+” “;
            }

            return userNames;
        }
      }



NOTE:
The sample project used for this exercise can be cloned from git: https://gitlab.com/greenhorn/jsf-jpa.git


Reference:

Webservice client 3: JSP Page in Web Application

This post is a continuation of the previous post:
Test and add method to webservice

Reference:

Webservice client 2: Servlet in Web Application

This post is a continuation of the previous post:
Test and add method to webservice

Reference:

Webservice client 1: Java Class in Java SE Application

This post is a continuation of the previous post:
Test and add method to webservice

Reference:

Test and add method to webservice

This post works based on a previous post:
Simple webservice with maven 3.0.3, Netbeans 7.3.1 and Glassfish 4

The content of the video
1. How to test a webservice?
2. How to change the context root in glassfish 4?
3. Adding feature to the existing webservice.

Reference:

Simple webservice with maven 3.0.3, Netbeans 7.3.1 and Glassfish 4

Reference:

Installing Glassfish4 on Windows 7

NOTE:
Glassfish4 requires java 7 to be installed on the Windows 7. If you haven’t installed java 7 already, then the Glassfish4 installation will fail.

Installing glassfish4 on Ubuntu 12.04 LTS

NOTE:
Glassfish4 requires java 7 to be installed on the Ubuntu. If you haven’t installed java 7 already, then the Glassfish4 installation will fail.

Installation directory used was:
usr/local/src/glassfish4

To start, from installation directory in a terminal:
sh bin/asadmin start-domain

To stop, from installation directory in a terminal:
sh bin/asadmin stop-domain

References: