linux下的Redis+spring+struts2实现点赞功能(八)

使用Redis实现点赞功能,其实就是利用了jredis中的incr()方法,
这个方法每运行一次,其中对应的值就会加1,默认从0开始。
更多关于redis的方法:http://langgufu.iteye.com/blog/1434408
思路:
可以将点赞和踩这个两个功能,看做一个实体类的属性,
后面其实就是对它属性的操作。
1,在前台页面通过Struts2发送请求到后台。
2,定义接口实现点赞功能,在业务层中由spring创建redis池,从其中取到一个jedis。
调用其incr(),来实现。
3,通过action调用业务层,并将数据返回到页面。
通过后面的代码展示会更加详细了解这个功能的实现。
效果展示:
linux下的Redis+spring+struts2实现点赞功能(八)_第1张图片

代码部分:
1,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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_9" version="2.4">
  <display-name>Struts Blank</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring*.xml</param-value>
  </context-param>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

2,spring配置:

<context:property-placeholder location="classpath:redis.properties" />  
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
    <property name="maxTotal"  value="${redis.pool.maxActive}" /> 
    <property name="maxIdle"    value="${redis.pool.maxIdle}" />  
    <property name="maxWaitMillis"    value="${redis.pool.maxWait}" />  
    <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />  
</bean>  
<bean  id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" >  
    <constructor-arg  index="0" ref="jedisPoolConfig" />  
    <constructor-arg index="1">  
        <list>  
            <bean class="redis.clients.jedis.JedisShardInfo">  
                <constructor-arg  
                    index="0"  
                    value="${redis.ip}" />  
                <constructor-arg  
                    index="1"  
                    value="${redis.port}"  
                    type="int" />  
            </bean>  

        </list>  
    </constructor-arg>  
</bean>  

3,struts2配置:

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="action,do,,"/>
<constant name="struts.objectFactory" value="spring" />

<package name="default" namespace="/" extends="json-default">

  <action name="User_*" class="com.yc.web.actions.UserAction" method="{1}">
    <result name="success" type="json">
            <param name="root">
                jsonModel
            </param>
        </result>
  </action>
</package>

4,redis.properties配置:
redis.pool.maxActive=1024
redis.pool.maxIdle=200
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.ip=192.168.14.215
redis.port=6379

5,jsp页面代码:

<style>
#head {
    font-family: cursive;
    font-size: 25px;
    color: orange;
    cursor: pointer;
}

#content {height =300px;
    font-family: cursive;
    font-size: 20px;
}

.ding {
    border: 1px solid blue;
    height: 60px;
    width: 70px;
    margin-left: 150px; padding-left10px;
    float: left;
    background: #FFF;
}

.ding img {
    display: inline-block;
    margin-top: 15px;
    margin-left: 10px;
    cursor: pointer;
}

.sding {
    float: left;
    margin-top: 16px;
    font-family: cursive;
    text-decoration: underline;
}
</style>
<script src="js/jquery-1.11.3.js"></script>
<script type="text/javascript">
    //当这个页面加载完毕 则给 loginbtn绑定单击事件
    $(function() {
        $.ajax({
            type : "POST",
            url : "User_Find.action",
            dataType : 'JSON',
            success : function(data) {
                if (data.code == 1) {
                    $("#num").text(data.obj.ding);
                    $("#numcai").text(data.obj.cai);
                } else {
                    alert(data);
                }
            }

        });
    });
    function addDing() {

        $.ajax({
            type : "POST",
            url : "User_Ding.action",
            dataType : 'JSON',
            success : function(data) {
                if (data.code == 1) {
                    $("#num").text(data.obj.ding)
                } else {
                    alert(data);
                }
            }

        });
    }

function addCai(){
        $.ajax({
            type:"POST",
            url:"User_Cai.action",
            dataType:'JSON',
            success: function(data){
                if(data.code==1){
                    $("#numcai").text(data.obj.cai);
                }else{
                    alert(data);
                }
            }

        });
    }
</script>

</head>



<body>
    <center>
        <div style="background: #0F0; height: 400px; width: 600px;">
            <table
                CELLPADDING="0"
                border="1px"
                bordercolor="yellow"
                height="400px"
                width="600px">

                <tr>
                    <th
                        id="head"
                        align="left"
                        height="50px"
                        colspan="3">年度最热门博客</th>
                </tr>
                <tr id="content">

                    <td
                        height="200"
                        colspan="3">都说飞得越高,摔得越惨,但是我想看看我到底会摔得有多惨,落地了,只要没死,那么爬起来,每走一步都是向上! <br>
                        </br>
                        时间并不会真的帮我们解决什么问题,它只是把原来怎么也想不通的问题,变得不再重要了。
                        </td>
                </tr>
                <tr>
                    <td colspan="3">
                        <div class="ding">
                            <span class="sding">顶</span><img
                                alt="tup"
                                src="images/ding.gif"
                                onclick="addDing()"><span id="num"></span>
                        </div>

                        <div class="ding">
                            <span class="sding">踩</span><img
                                alt="tup"
                                src="images/cai.gif" onclick="addCai()"><span id="numcai"></span>
                        </div>

                    </td>

                </tr>


            </table>
        </div>
    </center>

</body>


6,接口代码:
public interface UserService {
    public String Ding();
    public String Cai() ;
    public String[] Find() ;
}

7,实现类,即业务层代码:
@Service(“userServiceImpl”)
public class UserServiceImpl implements UserService {
@Autowired
private ShardedJedisPool shardedJedisPool;
@Override
public String Ding() {
ShardedJedis jedis=shardedJedisPool.getResource();
jedis.incr(“User:ding”);
// 获取存储的数据并输出
String num = jedis.get(“User:ding”);
return num;
}

@Override
public String Cai() {
    // 设置 redis 字符串数据
    ShardedJedis jedis=shardedJedisPool.getResource();
    jedis.incr("User:cai");

    // // 获取存储的数据并输出
    String num = jedis.get("User:cai");
    return num;
}

@Override
public String[] Find() {
    ShardedJedis jedis=shardedJedisPool.getResource();
    // // 获取存储的数据并输出
    String s[]=new String[2];
    s[0] = jedis.get("User:ding");
    s[1] = jedis.get("User:cai");
    return s;
}

public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
    this.shardedJedisPool = shardedJedisPool;
}

}

8,action处理代码:
@Controller(“userAction”)
public class UserAction extends ActionSupport implements ModelDriven {
private User user;

/**
 * 
 */
private static final long serialVersionUID = 1825223373127715747L;
private JsonModel jsonModel = new JsonModel();
@Autowired
private UserService userServiceImpl;

public JsonModel getJsonModel() {
    return jsonModel;
}

public void setJsonModel(JsonModel jsonModel) {
    this.jsonModel = jsonModel;
}

public String Ding() {
    //User user = new User();
    //UserService userService=new UserServiceImpl();
    //System.out.println("ding 来了。。。。");
    String num=userServiceImpl.Ding();
    if(num!=null&&!"".equals(num)){
        user.setDing(Integer.parseInt(num));
        jsonModel.setCode(1);
        jsonModel.setObj(user);
    }
    return SUCCESS;
}

public String Cai() {
    //User user = new User();
    //UserService userService=new UserServiceImpl();
    String num=userServiceImpl.Cai();
    if(num!=null&&!"".equals(num)){
        user.setCai(Integer.parseInt(num));
        jsonModel.setCode(1);
        jsonModel.setObj(user);
    }
    return SUCCESS;
}

public String Find() {
    //User user = new User();
    //UserService userService=new UserServiceImpl();
    String num[]=userServiceImpl.Find();
    user.setCai(Integer.parseInt(num[1]));
    user.setDing(Integer.parseInt(num[0]));
    jsonModel.setCode(1);
    jsonModel.setObj(user);
    return SUCCESS;

}


@Override
public User getModel() {
    user=new User();
    return user;
}

}

9,实体类:
public class User {
private int ding;

private int cai;

public int getCai() {
    return cai;
}

public void setCai(int cai) {
    this.cai = cai;
}

public int getDing() {
    return ding;
}

public void setDing(int ding) {
    this.ding = ding;
}

}

你可能感兴趣的:(redis,spring,linux,struts2.0)