Web Weather Application


This one is not really finished yet. I still want to make a daily or/and hourly forecast. That is why so much space was left empty.

Model was created from  my weather application , that I've done before, and Controller and View were developed rather quickly which resulted in not so spectacular UI and functionalities.

djordje.webweatherapp.model.Weather.java
 /*  
  * Web Weather Application - Maven / Spring Web MVC  
  */  
 package djordje.webweatherapp.model;  
 import net.aksingh.owmjapis.CurrentWeather;  
 import net.aksingh.owmjapis.OpenWeatherMap;  
 import org.json.JSONException;  
 /**  
  *  
  * @author djordje gavrilovic  
  */  
 public class Weather {  
   private int cityID;  
   private String cityName;  
   private String description;  
   private String temperature;  
   private String pressure;  
   private String humidity;  
   private String wind;  
   private String iconID;  
   public int getCityID() {return cityID;}  
   public void setCityID(int cityID) {this.cityID = cityID;}  
   public String getCityName() {return cityName;}  
   public void setCityName(String cityName) {this.cityName = cityName;}  
   public String getDescription() {return description;}  
   public void setDescription(String description) {this.description = description;}  
   public String getTemperature() {return temperature;}  
   public void setTemperature(String temperature) {this.temperature = temperature;}  
   public String getPressure() {return pressure;}  
   public void setPressure(String pressure) {this.pressure = pressure;}  
   public String getHumidity() {return humidity;}  
   public void setHumidity(String humidity) {this.humidity = humidity;}  
   public String getWind() {return wind;}  
   public void setWind(String wind) {this.wind = wind;}  
   public String getIconID() {return iconID;}  
   public void setIconID(String iconID) {this.iconID = iconID;}  
   public Weather(){}  
   public Weather(int cityID) throws JSONException{  
     this.cityID = cityID;  
     getWeather();  
   }  
   // gets weather data from OWM  
   public void getWeather() throws JSONException{  
     OpenWeatherMap wm = new OpenWeatherMap("**************************"); // personal OWM API KEY  
     CurrentWeather cw = wm.currentWeatherByCityCode(cityID);  
     this.cityName = cw.getCityName();  
     this.description = cw.getWeatherInstance(0).getWeatherDescription();  
     float tempC = (((cw.getMainInstance().getTemperature())-32)*5)/9;  
     this.temperature = String.format("%.1f",tempC);  
     this.pressure = String.valueOf(cw.getMainInstance().getPressure());  
     this.humidity = String.valueOf(cw.getMainInstance().getHumidity());  
     float kmph = (cw.getWindInstance().getWindSpeed()) * 1.609F;  
     this.wind = String.format("%.1f",kmph);  
     this.iconID = cw.getWeatherInstance(0).getWeatherIconName();          
   }  
   @Override  
   public String toString() {  
     return   
         "ID: " + cityID + "\n" +   
         "City Name: " + cityName + "\n" +   
         "Description: " + description + "\n" +   
         "Temperature: " + temperature + " C" + "\n" +   
         "Pressure: " + pressure + " mbar" + "\n" +   
         "Humidity: " + humidity + " %" + "\n" +  
         "Wind: " + wind + " kmph" + "\n" +  
         "Icon Name: " + iconID;  
   }  
 }  
djordje.webweatherapp.WeatherController.java
 package djordje.webweatherapp;  
 /**  
  *  
  * @author djordje gavrilovic  
  */  
 import djordje.webweatherapp.model.Weather;  
 import org.springframework.stereotype.Controller;  
 import org.springframework.ui.ModelMap;  
 import org.springframework.web.bind.annotation.RequestMapping;  
 import org.springframework.web.bind.annotation.RequestMethod;  
 import org.springframework.web.bind.annotation.RequestParam;  
 @Controller  
 public class WeatherController {  
   Weather weather;  
   @RequestMapping (value="/", method=RequestMethod.GET)  
   public String doWeather(@RequestParam(value="city", required=false, defaultValue="BajinaBasta")  
       String city, ModelMap model) {  
     switch(city){  
       case "BajinaBasta":  
         weather = new Weather(3204623);  
         weather.getWeather();  
         model.addAttribute("weatherIcon", "http://openweathermap.org/img/w/"+weather.getIconID()+".png");  
         model.addAttribute("cityName", weather.getCityName());  
         model.addAttribute("description", weather.getDescription());  
         model.addAttribute("temperature", weather.getTemperature());  
         model.addAttribute("pressure", weather.getPressure());  
         model.addAttribute("humidity", weather.getHumidity());  
         model.addAttribute("wind", weather.getWind());  
         model.addAttribute("textWeather", weather.toString());  
         break;  
       case "Belgrade":  
         weather = new Weather(792680);  
         weather.getWeather();  
         model.addAttribute("weatherIcon", "http://openweathermap.org/img/w/"+weather.getIconID()+".png");  
         model.addAttribute("cityName", weather.getCityName());  
         model.addAttribute("description", weather.getDescription());  
         model.addAttribute("temperature", weather.getTemperature());  
         model.addAttribute("pressure", weather.getPressure());  
         model.addAttribute("humidity", weather.getHumidity());  
         model.addAttribute("wind", weather.getWind());  
         model.addAttribute("textWeather", weather.toString());  
         break;  
       case "Zrenjanin":  
         weather = new Weather(783814);  
         weather.getWeather();  
         model.addAttribute("weatherIcon", "http://openweathermap.org/img/w/"+weather.getIconID()+".png");  
         model.addAttribute("cityName", weather.getCityName());  
         model.addAttribute("description", weather.getDescription());  
         model.addAttribute("temperature", weather.getTemperature());  
         model.addAttribute("pressure", weather.getPressure());  
         model.addAttribute("humidity", weather.getHumidity());  
         model.addAttribute("wind", weather.getWind());  
         model.addAttribute("textWeather", weather.toString());  
         break;  
     }  
     return "weather";  
   }  
 }  

Web Pages/WEB-INF/jsp/Weather.jsp
 <%--   
   Document  : weather  
   Created on : Oct 22, 2017, 8:11:44 PM  
   Author   : djordje gavrilovic  
 --%>  
 <%@page contentType="text/html" pageEncoding="UTF-8"%>  
 <!DOCTYPE html>  
 <html>  
   <head>  
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
     <link href="${pageContext.request.contextPath}/resources/css/weather1.css" type="text/css" rel="stylesheet">  
     <title>Weather Page</title>  
   </head>  
   <body>   
     <div id="allDiv">  
       <form method="get" commandName="weather" id="forma">  
         <input type="radio" name="city" id="BajinaBasta" value="BajinaBasta"> Bajina Basta   
         <input type="radio" name="city" id="Belgrade" value="Belgrade"> Belgrade  
         <input type="radio" name="city" id="Zrenjanin" value="Zrenjanin"> Zrenjanin  
         <input type="submit" value="Load" />  
       </form>  
       <br/>  
       <img src=${weatherIcon} alt="Weather Icon">  
       <h1>${cityName}</h1>  
       <h4>${description}</h4>  
       <h2>${temperature} &#8451;</h2>  
       <p>Pressure: ${pressure} mbar.</p>  
       <p>Humidity: ${humidity} %</p>  
       <p>Wind: ${wind} km/h</p>  
       <p id="sub">Djordje Gavrilovic 2017. Powered by OpenWeatherMap</p>  
     </div>  
   </body>  
 </html>  

Web Pages/resources/css/Weather1.css
 body {  
   color: white;  
   background-image: url("http://www.designbolts.com/wp-content/uploads/2014/03/Bright-Blue1.jpg");  
 }  
 #allDiv {  
   width: 600px;  
   height: 600px;  
   background-color: rgba(0,0,0,0.4);  
   position: absolute;  
   top:0;  
   bottom: 0;  
   left: 0;  
   right: 0;  
   margin: auto;  
   padding: 15px;  
   line-height: 12px;  
 }  
 img {  
   width: 60px;  
   height: 60px;  
 }  
 h2 {  
   font-size: 50px;  
 }  
 #sub {  
   position: absolute;  
   bottom: 0;  
   right: 15px;  
   font-size: 10px;  
   color: rgba(0,0,0,0.6);  
 }  

applicationContext.xml
 <?xml version='1.0' encoding='UTF-8' ?>  
 <!-- was: <?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:aop="http://www.springframework.org/schema/aop"  
     xmlns:tx="http://www.springframework.org/schema/tx"  
     xmlns:mvc="http://www.springframework.org/schema/mvc"  
     xmlns:context="http://www.springframework.org/schema/context"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
   <context:component-scan base-package="djordje.webweatherapp" />   
   <mvc:annotation-driven />  
   <mvc:resources location="/resources/" mapping="/resources/**"/>  
 </beans>  

dispatcher-servlet.xml
 <?xml version='1.0' encoding='UTF-8' ?>  
 <!-- was: <?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:aop="http://www.springframework.org/schema/aop"  
     xmlns:tx="http://www.springframework.org/schema/tx"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
   <bean id="viewResolver"  
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
      p:prefix="/WEB-INF/jsp/"  
      p:suffix=".jsp" />   
 </beans>  

web.xml
 <?xml version="1.0" encoding="UTF-8"?>   
 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
       version="3.1">  
   <context-param>  
     <param-name>contextConfigLocation</param-name>  
     <param-value>/WEB-INF/applicationContext.xml</param-value>  
   </context-param>  
   <listener>  
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
   </listener>  
   <servlet>  
     <servlet-name>dispatcher</servlet-name>  
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
     <load-on-startup>2</load-on-startup>  
   </servlet>  
   <servlet-mapping>  
     <servlet-name>dispatcher</servlet-name>  
     <url-pattern>/</url-pattern>  
   </servlet-mapping>  
   <session-config>  
     <session-timeout>  
       30  
     </session-timeout>  
   </session-config>  
 </web-app>  

pom.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <project xmlns="http://maven.apache.org/POM/4.0.0"   
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
   <modelVersion>4.0.0</modelVersion>  
   <groupId>djordje</groupId>  
   <artifactId>WebWeatherApp</artifactId>  
   <version>1.0-SNAPSHOT</version>  
   <packaging>war</packaging>  
   <name>WebWeatherApp</name>  
   <properties>  
     <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>  
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
     <spring.version>4.0.1.RELEASE</spring.version>  
   </properties>  
   <dependencies>  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-context</artifactId>  
       <version>4.0.1.RELEASE</version>  
       <type>jar</type>  
     </dependency>  
     <dependency>  
       <groupId>javax</groupId>  
       <artifactId>javaee-web-api</artifactId>  
       <version>7.0</version>  
       <scope>provided</scope>  
     </dependency>  
     <!--Spring Dependencies-->  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-core</artifactId>  
       <version>${spring.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-web</artifactId>  
       <version>${spring.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-webmvc</artifactId>  
       <version>${spring.version}</version>  
     </dependency>  
     <!-- OWM Dependency -->  
     <dependency>  
       <groupId>net.aksingh</groupId>  
       <artifactId>owm-japis</artifactId>  
       <version>2.5.0.3</version>  
     </dependency>  
     <!-- https://mvnrepository.com/artifact/org.json/json -->  
     <dependency>  
       <groupId>org.json</groupId>  
       <artifactId>json</artifactId>  
       <version>20160810</version>  
     </dependency>  
   </dependencies>  
   <build>  
     <plugins>  
       <plugin>  
         <groupId>org.apache.maven.plugins</groupId>  
         <artifactId>maven-compiler-plugin</artifactId>  
         <version>3.1</version>  
         <configuration>  
           <source>1.7</source>  
           <target>1.7</target>  
           <compilerArguments>  
             <endorseddirs>${endorsed.dir}</endorseddirs>  
           </compilerArguments>  
         </configuration>  
       </plugin>  
       <plugin>  
         <groupId>org.apache.maven.plugins</groupId>  
         <artifactId>maven-war-plugin</artifactId>  
         <version>2.3</version>  
         <configuration>  
           <failOnMissingWebXml>false</failOnMissingWebXml>  
         </configuration>  
       </plugin>  
       <plugin>  
         <groupId>org.apache.maven.plugins</groupId>  
         <artifactId>maven-dependency-plugin</artifactId>  
         <version>2.6</version>  
         <executions>  
           <execution>  
             <phase>validate</phase>  
             <goals>  
               <goal>copy</goal>  
             </goals>  
             <configuration>  
               <outputDirectory>${endorsed.dir}</outputDirectory>  
               <silent>true</silent>  
               <artifactItems>  
                 <artifactItem>  
                   <groupId>javax</groupId>  
                   <artifactId>javaee-endorsed-api</artifactId>  
                   <version>7.0</version>  
                   <type>jar</type>  
                 </artifactItem>  
               </artifactItems>  
             </configuration>  
           </execution>  
         </executions>  
       </plugin>  
     </plugins>  
   </build>  
 </project>  




Comments

  1. can you send me the source code. i need it to complete my project on class. I'm appreciate about that. Thank you

    ReplyDelete

Post a Comment