MongoDB实现商品评论
一、从MongoDB获取商品评论信息
MongoDB配置文件:mongo.properties
host=127.0.0.1
port=27017
databaseName=zhiheng
collectionName=discuss
MongoDBUtil工具类
package com. java. web. util; import com. mongodb. MongoClient;
import com. mongodb. client. MongoCollection;
import com. mongodb. client. MongoDatabase;
import org. bson. Document;
import java. io. IOException;
import java. io. InputStream;
import java. util. Properties; public class MongoUtil { private static MongoCollection< Document> userCollection= null; private static MongoClient client = null; private static String host = null; private static Integer port = null; private static String databaseName = null; private static String collectionName = null; static { try { Properties prop = new Properties ( ) ; InputStream ins = MongoUtil. class . getClassLoader ( ) . getResourceAsStream ( "mongo.properties" ) ; prop. load ( ins) ; host = prop. getProperty ( "host" ) ; port = Integer. parseInt ( prop. getProperty ( "port" ) ) ; databaseName = prop. getProperty ( "databaseName" ) ; collectionName = prop. getProperty ( "collectionName" ) ; ins. close ( ) ; } catch ( IOException e) { e. printStackTrace ( ) ; } } public static MongoCollection< Document> getCollection ( ) { client = new MongoClient ( host, port) ; MongoDatabase db = client. getDatabase ( databaseName) ; userCollection = db. getCollection ( collectionName) ; return userCollection; } public static void close ( ) { if ( client!= null) client. close ( ) ; }
}
评论业务层接口
package com. java. web. service; import org. bson. Document;
import java. util. List;
public interface DiscussService { List< Document> findPageDiscuss ( Integer page, Integer limit) throws Exception;
}
评论业务层实现类
package com. java. web. service. impl; import com. java. web. service. DiscussService;
import com. java. web. util. MongoUtil;
import com. mongodb. client. FindIterable;
import com. mongodb. client. MongoCollection;
import org. bson. Document;
import org. springframework. stereotype. Service;
import org. springframework. transaction. annotation. Transactional; import java. util. ArrayList;
import java. util. List;
@Service
@Transactional ( readOnly = false )
public class DiscussServiceImpl implements DiscussService { @Override public List< Document> findPageDiscuss ( Integer page, Integer limit) throws Exception { MongoCollection< Document> discussCollection = MongoUtil. getCollection ( ) ; FindIterable< Document> documents = discussCollection. find ( ) ; documents. skip ( ( page - 1 ) * limit) . limit ( limit) ; ArrayList< Document> list = new ArrayList < > ( ) ; documents. iterator ( ) . forEachRemaining ( temp- > System. out. println ( list. add ( temp) ) ) ; return list; }
}
评论控制器层
package com. java. web. controller; import com. java. web. service. DiscussService;
import org. bson. Document;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Controller;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. ResponseBody; import java. util. List;
@Controller
@RequestMapping ( "/discuss" )
public class DiscussController { @Autowired private DiscussService discussService; @RequestMapping ( "/loadPageDiscuss" ) public @ResponseBody List< Document> loadPageDiscuss ( Integer page, Integer limit) { try { return discussService. findPageDiscuss ( page, limit) ; } catch ( Exception e) { e. printStackTrace ( ) ; return null; } }
}
二、使用过滤器工具解决跨域问题
过滤器工具
package com. java. web. filter; import javax. servlet. *;
import javax. servlet. annotation. WebFilter;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
import java. io. IOException;
@WebFilter ( urlPatterns = { "/*" } )
public class KuayuFilter implements Filter { @Override public void init ( FilterConfig filterConfig) throws ServletException { System. out. println ( "KuayuFilter初始化成功了。。。" ) ; / / 测试用} @Override public void doFilter ( ServletRequest res, ServletResponse req, FilterChain chain) throws IOException, ServletException { System. out. println ( "KuayuFilter执行了。。。" ) ; / / 测试用HttpServletRequest request = ( HttpServletRequest) res; HttpServletResponse response= ( HttpServletResponse) req; / / 允许跨域访问response. setHeader ( "Access-Control-Allow-Origin" , "*" ) ; / / 设置允许所有跨域访问response. setHeader ( "Access-Control-Allow-Methods" , "POST,GET,PUT,OPTIONS,DELETE" ) ; response. setHeader ( "Access-Control-Max-Age" , "3600" ) ; response. setHeader ( "Access-Control-Allow-Headers" , "Origin,X-Requested-With,Content-Type,Accept,Authorization,token" ) ; response. setHeader ( "Access-Control-Allow-Credentials" , "true" ) ; / / 放行chain. doFilter ( request, response) ; } @Override public void destroy ( ) { System. out. println ( "KuayuFilter销毁了。。。" ) ; / / 测试用}
}
WebStart启动器添加注解使跨域工具生效
@ServletComponentScan ( basePackages = "com.java.web.filter" )
三、将商品评论信息整合到显示页面
Product.ftl 模板
< script type= "text/javascript" > jQuery ( function ( ) { jQuery ( "#p_comment" ) . click ( function ( ) { jQuery. ajax ( { type: 'GET' , url: 'http://localhost:8083/discuss/loadPageDiscuss' , dataType: 'JSON' , data: { "page" : 1 , "limit" : 3 } , success: function ( data) { var content = "" ; for ( var i in data) { content+= "\n" + " "+ data[ i] . userName+ " \n" + " \n" + "\t颜色分类:" + data[ i] . color+ " \n" + "\t型号:" + data[ i] . type+ "\n" + " \n" + " \n" + "\t" + data[ i] . content+ " \n" + "\t" + data[ i] . dateTime+ "\n" + " \n" + " " } jQuery ( ".jud_list" ) . html ( content) ; } , error: function ( data) { alert ( "服务器异常" ) } } ) } ) } )
< / script>
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】 进行投诉反馈!