Skip to content

Commit b452687

Browse files
committed
1 parent 5340c12 commit b452687

File tree

8 files changed

+221
-12
lines changed

8 files changed

+221
-12
lines changed

eshop8_2015/pom.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@
6464
<artifactId>spring-web</artifactId>
6565
<version>3.2.13.RELEASE</version>
6666
</dependency>
67-
67+
<dependency>
68+
<groupId>org.springframework</groupId>
69+
<artifactId>spring-oxm</artifactId>
70+
<version>3.2.13.RELEASE</version>
71+
</dependency>
6872
<dependency>
6973
<groupId>org.springframework</groupId>
7074
<artifactId>spring-webmvc</artifactId>
@@ -80,6 +84,11 @@
8084
<artifactId>hibernate-validator</artifactId>
8185
<version>4.3.1.Final</version>
8286
</dependency>
87+
<dependency>
88+
<groupId>org.codehaus.jackson</groupId>
89+
<artifactId>jackson-mapper-asl</artifactId>
90+
<version>1.9.5</version>
91+
</dependency>
8392
</dependencies>
8493

8594
<repositories>

eshop8_2015/src/main/java/com/eshop/dao/Product.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package com.eshop.dao;
22

3+
import javax.xml.bind.annotation.XmlRootElement;
4+
5+
@XmlRootElement
36
public class Product {
47
private int id;
58
private String type;
69
private String name;
710
private double price;
811

12+
public Product() {
13+
14+
}
15+
916
public Product(int id, String type, String name, double price) {
1017
super();
1118
this.id = id;
@@ -29,4 +36,20 @@ public String getName() {
2936
public double getPrice() {
3037
return price;
3138
}
39+
40+
public void setId(int id) {
41+
this.id = id;
42+
}
43+
44+
public void setType(String type) {
45+
this.type = type;
46+
}
47+
48+
public void setName(String name) {
49+
this.name = name;
50+
}
51+
52+
public void setPrice(double price) {
53+
this.price = price;
54+
}
3255
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.eshop.dao;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.xml.bind.annotation.XmlElement;
7+
import javax.xml.bind.annotation.XmlRootElement;
8+
9+
@XmlRootElement(name = "products")
10+
public class Products {
11+
12+
private List<Product> products = new ArrayList<Product>();
13+
14+
@XmlElement(name = "product")
15+
public List<Product> getProducts() {
16+
return products;
17+
}
18+
19+
public void addProduct(Product product) {
20+
this.products.add(product);
21+
}
22+
23+
public void setProducts(List<Product> products) {
24+
this.products = products;
25+
}
26+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.eshop.web;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.ResponseBody;
12+
import org.springframework.web.servlet.ModelAndView;
13+
14+
import com.eshop.dao.Product;
15+
import com.eshop.dao.Products;
16+
import com.eshop.service.ProductsService;
17+
18+
@Controller
19+
@RequestMapping("/product/search")
20+
public class ProductSearchController {
21+
22+
@Autowired
23+
private ProductsService service;
24+
25+
@RequestMapping("suggestions")
26+
@ResponseBody
27+
public Products getSuggestions(@RequestParam String key) {
28+
List<Product> allProducts = service.getAllProducts();
29+
30+
Products products = new Products();
31+
32+
for(Product product : allProducts) {
33+
if(product.getName().startsWith(key)) {
34+
products.addProduct(product);
35+
}
36+
}
37+
38+
return products;
39+
}
40+
41+
@RequestMapping(value = "/", method=RequestMethod.POST)
42+
public ModelAndView homePage(@RequestParam String productSearch) {
43+
System.out.println("Real search has been called");
44+
List<Product> allProducts = service.getAllProducts();
45+
46+
List<Product> resultProducts = new ArrayList<Product>();
47+
48+
for(Product product : allProducts) {
49+
if(product.getName().startsWith(productSearch)) {
50+
resultProducts.add(product);
51+
}
52+
}
53+
54+
ModelAndView modelAndView = new ModelAndView();
55+
modelAndView.setViewName("Home");
56+
modelAndView.addObject("AllProducts", resultProducts);
57+
58+
return modelAndView;
59+
}
60+
}

eshop8_2015/src/main/webapp/WEB-INF/eshop-servlet.xml

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,36 @@
1010

1111
<mvc:resources location="/styles/" mapping="/styles/*"/>
1212
<mvc:resources location="/images/" mapping="/images/*"/>
13+
<mvc:resources location="/scripts/" mapping="/scripts/*"/>
1314

1415
<context:component-scan base-package="com.eshop"></context:component-scan>
1516

16-
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17-
<property name="prefix" value="/WEB-INF/jsp/"></property>
18-
<property name="suffix" value=".jsp"></property>
17+
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
18+
<property name="contentNegotiationManager" ref="contentNegotiationManager"></property>
1919
</bean>
20-
20+
21+
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
22+
<property name="defaultContentType" value="text/html"></property>
23+
<property name="ignoreAcceptHeader" value="true"></property>
24+
</bean>
25+
26+
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
27+
<property name="prefix" value="/WEB-INF/jsp/" />
28+
<property name="suffix" value=".jsp" />
29+
</bean>
30+
31+
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
32+
33+
<bean class= "org.springframework.web.servlet.view.xml.MarshallingView">
34+
<constructor-arg ref="jaxbMarshaller" />
35+
</bean>
36+
37+
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
38+
<property name="classesToBeBound">
39+
<list>
40+
<value>com.eshop.dao.Product</value>
41+
<value>com.eshop.dao.Products</value>
42+
</list>
43+
</property>
44+
</bean>
2145
</beans>

eshop8_2015/src/main/webapp/WEB-INF/jsp/Home.jsp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@
88
<title>Insert title here</title>
99

1010
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
11-
<link rel="stylesheet" href="/eshop/styles/eshop.css"/>
11+
<link rel="stylesheet" href="/eshop9/styles/eshop.css"/>
1212
</head>
1313

1414
<body id="homePageBody">
1515
<%@include file="Header.jsp" %>
1616

1717
<div class="container">
18-
<h2 id="mainHeading">Welcome to eShop</h2>
18+
19+
<form action="/eshop9/product/search/" class="form-horizantal" method="post">
20+
<div class="form-group form-inline row">
21+
<input type="text" id="productSearch" name="productSearch" class="form-control">
22+
<button type="submit" class="btn btn-default" id="productSearch">
23+
<span class="glyphicon glyphicon-search"></span> Search
24+
</button>
25+
</div>
26+
</form>
27+
28+
<div id="searchResults"></div>
1929

2030
<table id="allProducts" class="table">
2131
<tr>
@@ -37,9 +47,9 @@
3747
</c:forEach>
3848
</table>
3949

40-
<div class="row footer">
41-
<div class="col-xs-4">
42-
<h2 class="text-center">Current Deals</h2>
50+
<div class="row row-eq-height">
51+
<div class="col-xs-4" style="background-color: #F5F5F0;">
52+
<h2 class="text-center col-full-height">Current Deals</h2>
4353

4454
<ul>
4555
<li>Diet Coke $.50</li>
@@ -52,7 +62,7 @@
5262
<h2>Coupons</h2>
5363
</div>
5464

55-
<div class="col-xs-3 text-center">
65+
<div class="col-xs-3 text-center" style="background-color: #F5F5F0;">
5666
<h2>Contact Us</h2>
5767
</div>
5868
</div>
@@ -62,6 +72,7 @@
6272

6373
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
6474
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
75+
<script src="/eshop9/scripts/application.js"></script>
6576

6677
</body>
6778
</html>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
$(document).ready(function() {
2+
3+
$("#productSearch").keyup(function() {
4+
5+
var searchKey = $("#productSearch").val();
6+
7+
//If user didn't enter anything, do not go to server. Just empty the html in the search results div.
8+
if(searchKey == '') {
9+
$("#searchResults").html("");
10+
return;
11+
}
12+
13+
var data = {"key" : searchKey};
14+
15+
$.getJSON('http://localhost:9090/eshop9/product/search/suggestions.json', data, function(responseData) {
16+
17+
var searchResultsHtml = "";
18+
19+
var resultsArray = responseData.products;
20+
21+
$.each(resultsArray, function(index, product) {
22+
23+
var idOfAnchorTag = "searchResult_"+index;
24+
25+
searchResultsHtml += "<a href='#' id='" + idOfAnchorTag + "' onclick=\"copyIntoSearchBox('" + idOfAnchorTag + "')\">" + product.name + "</a><br/>";
26+
27+
});
28+
29+
//Write the final HTML into search results div
30+
$("#searchResults").html(searchResultsHtml);
31+
32+
});
33+
34+
});
35+
});
36+
37+
function copyIntoSearchBox(id) {
38+
39+
console.log(id);
40+
41+
var jQueryId = "#"+id;
42+
console.log(jQueryId);
43+
44+
var selectedAnchorTagValue = $(jQueryId).text();
45+
console.log('selectedAnchorTagValue = ' + selectedAnchorTagValue);
46+
47+
$("#productSearch").val(selectedAnchorTagValue);
48+
}

eshop8_2015/src/main/webapp/styles/eshop.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2+
.row-eq-height {
3+
display: -webkit-box;
4+
display: -webkit-flex;
5+
display: -ms-flexbox;
6+
display: none;
7+
}
8+
19
table, td, th {
210
border: 1px solid green;
311
}
@@ -40,7 +48,7 @@ label {
4048
width: 100px;
4149
}
4250

43-
.glyphicon {
51+
.glyphicon-shopping-cart {
4452
font-size: 28px;
4553
color: green;
4654
}

0 commit comments

Comments
 (0)