Today, I learned about Jedis, and then I made a case, but there was a mistake, and then I Baidu didn't solve it all night. I thought to see if I could have a big guy to help me see where the problem was. Baidu was a bit confused all night. Please help me out. I want to thank you very much in this place. Let's see. Thank you~
Here are the case requirements:
Case requirements:
1. Provide the index.html page with a drop-down list of provinces
2. When the page is loaded, send an ajax request to load all provinces
What's wrong with the browser? I'm confused. I want to learn to give up. But I won't give up. Please help me see what's wrong
Error in chrome browser
Error in Microsoft edge browser
Here is my code
1 package cn.local.domain;
2
3 public class Province {
4 private int id;
5 private String name;
6
7 public Province() {
8 }
9
10 public Province(int id, String name) {
11 this.id = id;
12 this.name = name;
13 }
14
15 public int getId() {
16 return id;
17 }
18
19 public void setId(int id) {
20 this.id = id;
21 }
22
23 public String getName() {
24 return name;
25 }
26
27 public void setName(String name) {
28 this.name = name;
29 }
30
31 @Override
32 public String toString() {
33 return "Province{" +
34 "id=" + id +
35 ", name='" + name + '\'' +
36 '}';
37 }
38 }
Database class:
1 package cn.local.dao;
2
3 import cn.local.domain.Province;
4
5 import java.util.List;
6
7 public interface ProvinceDao {
8 public List<Province> findAll();
9
10 }
1 package cn.local.dao.impl;
2
3 import cn.local.dao.ProvinceDao;
4 import cn.local.domain.Province;
5 import cn.local.util.JDBCUtils;
6 import org.springframework.jdbc.core.BeanPropertyRowMapper;
7 import org.springframework.jdbc.core.JdbcTemplate;
8
9 import java.util.List;
10
11 public class ProvinceDaoImpl implements ProvinceDao {
12 //1.Declare member variables jdbctemplement
13 private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
14
15 @Override
16 public List<Province> findAll() {
17 //1.Definition sql
18 String sql = "select * from province ";
19 //2.implement sql
20 List<Province> list = template.query(sql, new BeanPropertyRowMapper<Province>(Province.class));
21 return list;
22 }
23 }
JDBC tool class
1 package cn.local.util;
2
3 import com.alibaba.druid.pool.DruidDataSourceFactory;
4
5 import javax.sql.DataSource;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.sql.Connection;
9 import java.sql.SQLException;
10 import java.util.Properties;
11
12 public class JDBCUtils {
13 private static DataSource ds ;
14
15 static {
16
17 try {
18 //1.Load profile
19 Properties pro = new Properties();
20 //Use ClassLoader Load configuration file, get byte input stream
21 InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
22 pro.load(is);
23
24 //2.Initialize connection pool object
25 ds = DruidDataSourceFactory.createDataSource(pro);
26
27 } catch (IOException e) {
28 e.printStackTrace();
29 } catch (Exception e) {
30 e.printStackTrace();
31 }
32 }
33
34 /**
35 * Get connection pool object
36 */
37 public static DataSource getDataSource(){
38 return ds;
39 }
40
41
42 /**
43 * Get Connection object
44 */
45 public static Connection getConnection() throws SQLException {
46 return ds.getConnection();
47 }
48 }
Jedis utility class
1 package cn.local.jedis;
2
3 import redis.clients.jedis.Jedis;
4 import redis.clients.jedis.JedisPool;
5 import redis.clients.jedis.JedisPoolConfig;
6
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.util.Properties;
10
11 public class JedisPoolUtils {
12 private static JedisPool jedisPool;
13
14 static{
15 //Read profile
16 InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
17 //Establish Properties object
18 Properties pro = new Properties();
19 //Associated file
20 try {
21 pro.load(is);
22 } catch (IOException e) {
23 e.printStackTrace();
24 }
25 //Get data, set to JedisPoolConfig in
26 JedisPoolConfig config = new JedisPoolConfig();
27 config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
28 config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
29
30 //Initialization JedisPool
31 jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));
32
33
34
35 }
36
37
38 /**
39 * Get connection method
40 */
41 public static Jedis getJedis(){
42 return jedisPool.getResource();
43 }
44 }
Class service
1 package cn.local.service;
2
3 import cn.local.domain.Province;
4
5 import java.util.List;
6
7 public interface ProvinceService {
8 public List<Province> findAll();
9
10 public String findAllJson();
11 }
1 package cn.local.service.impl;
2
3 import cn.local.dao.ProvinceDao;
4 import cn.local.dao.impl.ProvinceDaoImpl;
5 import cn.local.domain.Province;
6 import cn.local.jedis.JedisPoolUtils;
7 import cn.local.service.ProvinceService;
8 import com.fasterxml.jackson.core.JsonProcessingException;
9 import com.fasterxml.jackson.databind.ObjectMapper;
10 import redis.clients.jedis.Jedis;
11
12
13 import java.util.List;
14
15 public class ProvinceServiceImpl implements ProvinceService {
16 //statement dao
17 private ProvinceDao dao = new ProvinceDaoImpl();
18
19 @Override
20 public List<Province> findAll() {
21 return dao.findAll();
22 }
23
24 /**
25 Using redis cache
26 */
27
28 @Override
29 public String findAllJson() {
30 //1.First from redis Query data in
31 //1.1 Obtain redis Client connection
32 Jedis jedis = JedisPoolUtils.getJedis();
33 String province_json = jedis.get("province");
34
35 //2 judge province_json Is the data null
36 if(province_json == null || province_json.length() == 0){
37 //redis No data in
38 System.out.println("redis No data in, query database...");
39 //2.1 Query from data
40 List<Province> ps = dao.findAll();
41 //2.2 take list Serialize to json
42 ObjectMapper mapper = new ObjectMapper();
43 try {
44 province_json = mapper.writeValueAsString(ps);
45 } catch (JsonProcessingException e) {
46 e.printStackTrace();
47 }
48
49 //2.3 take json Data storage redis
50 jedis.set("province",province_json);
51 //Return connection
52 jedis.close();
53
54 }else{
55 System.out.println("redis Data in, query cache...");
56 }
57
58
59 return province_json;
60 }
61 }
Class servlet
1 package cn.local.web.servlet;
2
3
4 import cn.local.service.ProvinceService;
5 import cn.local.service.impl.ProvinceServiceImpl;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.annotation.WebServlet;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import java.io.IOException;
13
14 @WebServlet("/provinceServlet")
15 public class ProvinceServlet extends HttpServlet {
16 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
17 /* //1.Call service query
18 ProvinceService service = new ProvinceServiceImpl();
19 List<Province> list = service.findAll();
20 //2.Serialize list to json
21 ObjectMapper mapper = new ObjectMapper();
22 String json = mapper.writeValueAsString(list);*/
23
24 //1.call service query
25 ProvinceService service = new ProvinceServiceImpl();
26 String json = service.findAllJson();
27
28
29 System.out.println(json);
30 //3.Response result
31 response.setContentType("application/json;charset=utf-8");
32 response.getWriter().write(json);
33
34 }
35
36 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
37 this.doPost(request, response);
38 }
39 }
Html file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
$(function () {
//Send out ajax Request, load all province data
$.get("provinceServlet",{},function (data) {
//[{"id":1,"name":"Beijing"},{"id":2,"name":"Shanghai"},{"id":3,"name":"Guangzhou"},{"id":4,"name":"Shaanxi"}]
//1.Obtain select
var province = $("#province");
//2.ergodic json array
$(data).each(function () {
//3.Establish<option>
var option = "<option name='"+this.id+"'>"+this.name+"</option>";
//4.call select Of append Append option
province.append(option);
});
});
});
</script>
</head>
<body>
<select id="province">
<option>--Please select a province--</option>
</select>
</body>
</html>
configuration file
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///jedis
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
host=localhost
port=6379
maxTotal=50
maxIdle=10