Java 實(shí)戰(zhàn)項(xiàng)目錘煉之在線購書商城系統(tǒng)的實(shí)現(xiàn)流程
一、項(xiàng)目簡述
功能:一個基于JavaWeb的網(wǎng)上書店的設(shè)計與實(shí)現(xiàn),歸納 出了幾個模塊,首先是登錄注冊模塊,圖書查找模塊,購 物車模塊,訂單模塊,個人中心模塊,用戶管理模塊,圖 書管理模塊等。 該項(xiàng)目是javaJeb技術(shù)的實(shí)戰(zhàn)操作,采用了MVC設(shè)計模 式,包括基本的entity, jscript, servlet,以及ajax異步請 求,查詢分頁,持久化層方法的封裝等等,對javaweb技 術(shù)的鞏固很有幫助,為J2EE的學(xué)習(xí)打下基礎(chǔ),適用于課程 設(shè)計,畢業(yè)設(shè)計。
二、項(xiàng)目運(yùn)行
環(huán)境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)。
項(xiàng)目技術(shù): JSP + Entity+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload 等等。




書信息管理代碼:
書信息管理:
@Controller
@RequestMapping("/book")
public class BookInfoController {
@Autowired
private IBookInfoService bookInfoService;
@Autowired
private BookDescMapper bookDescMapper;
/**
* 查詢某一本書籍詳情
*
* @param bookId
* @param model
* @return
*/
@RequestMapping("/info/{bookId}")
public String bookInfo(@PathVariable("bookId") Integer bookId, Model model) throws BSException {
//查詢書籍
BookInfo bookInfo = bookInfoService.findById(bookId);
//查詢書籍推薦列表
List<BookInfo> recommendBookList = bookInfoService.findBookListByCateId(bookInfo.getBookCategoryId(), 1, 5);
//查詢書籍詳情
BookDesc bookDesc = bookDescMapper.selectByPrimaryKey(bookId);
//增加訪問量
bookInfoService.addLookMount(bookInfo);
Collections.shuffle(recommendBookList);
model.addAttribute("bookInfo", bookInfo);
model.addAttribute("bookDesc", bookDesc);
model.addAttribute("recommendBookList", recommendBookList);
return "book_info";
}
/**
* 通過關(guān)鍵字和書籍分類搜索書籍列表
*
* @param keywords
* @return
*/
@RequestMapping("/list")
public String bookSearchList(@RequestParam(defaultValue = "", required = false) String keywords,
@RequestParam(defaultValue = "0", required = false) int cateId,//分類Id,默認(rèn)為0,即不按照分類Id查
@RequestParam(defaultValue = "1", required = false) int page,
@RequestParam(defaultValue = "6", required = false) int pageSize,
Model model) {
keywords = keywords.trim();
PageInfo<BookInfo> bookPageInfo = bookInfoService.findBookListByCondition(keywords, cateId, page, pageSize,0);//storeId為0,不按照商店Id查詢
model.addAttribute("bookPageInfo", bookPageInfo);
model.addAttribute("keywords", keywords);
model.addAttribute("cateId", cateId);
return "book_list";
}
}
shiro安全框架配置代碼:
@Configuration
/**
* shiro安全框架
*/
public class ShiroConfig {
@Bean(name = "lifecycleBeanPostProcessor")
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
@Bean
public SecurityManager securityManager(EhCacheManager ehCacheManager) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(myShiroRealm());
//securityManager.setRememberMeManager(rememberMeManager());
securityManager.setCacheManager(ehCacheManager);
return securityManager;
}
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
//攔截器
filterChainDefinitionMap.put("/img/**", "anon");
filterChainDefinitionMap.put("/fonts/**", "anon");
filterChainDefinitionMap.put("/static/**", "anon");
filterChainDefinitionMap.put("/css/**", "anon");
filterChainDefinitionMap.put("/js/**", "anon");
filterChainDefinitionMap.put("/book/**", "anon");
filterChainDefinitionMap.put("/upload/**", "anon");
filterChainDefinitionMap.put("/page/**", "anon");
filterChainDefinitionMap.put("/user/info", "user");
filterChainDefinitionMap.put("/user/**", "anon");//用戶登錄注冊不需要權(quán)限
filterChainDefinitionMap.put("/index/**", "anon");//首頁放行
filterChainDefinitionMap.put("/", "anon");
//配置退出 過濾器,其中的具體的退出代碼Shiro已經(jīng)替我們實(shí)現(xiàn)了
filterChainDefinitionMap.put("/user/logout", "logout");
//<!-- 過濾鏈定義,從上向下順序執(zhí)行,一般將/**放在最為下邊 -->:這是一個坑呢,一不小心代碼就不好使了;
//<!-- authc:所有url都必須認(rèn)證通過才可以訪問; anon:所有url都都可以匿名訪問-->
//filterChainDefinitionMap.put("/admin/**", "roles[admin]");//perms[system]
filterChainDefinitionMap.put("/**", "authc");
// 如果不設(shè)置默認(rèn)會自動尋找Web工程根目錄下的"/login.jsp"頁面
shiroFilterFactoryBean.setLoginUrl("/page/login");
// 登錄成功后要跳轉(zhuǎn)的鏈接
shiroFilterFactoryBean.setSuccessUrl("/index");
//未授權(quán)界面;
shiroFilterFactoryBean.setUnauthorizedUrl("/403");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
@Bean
@DependsOn("lifecycleBeanPostProcessor")
public MyShiroRealm myShiroRealm() {
MyShiroRealm myShiroRealm = new MyShiroRealm();
myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher());
myShiroRealm.setCachingEnabled(true);
//啟用身份驗(yàn)證緩存,即緩存AuthenticationInfo信息,默認(rèn)false
myShiroRealm.setAuthenticationCachingEnabled(true);
//緩存AuthenticationInfo信息的緩存名稱 在ehcache.xml中有對應(yīng)緩存的配置
myShiroRealm.setAuthenticationCacheName("authenticationCache");
//啟用授權(quán)緩存,即緩存AuthorizationInfo信息,默認(rèn)false
myShiroRealm.setAuthorizationCachingEnabled(true);
//緩存AuthorizationInfo信息的緩存名稱 在ehcache.xml中有對應(yīng)緩存的配置
myShiroRealm.setAuthorizationCacheName("authorizationCache");
return myShiroRealm;
}
/**
* 開啟shiro aop注解支持.
* 使用代理方式;所以需要開啟代碼支持;
* @param securityManager
* @return
*/
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
return authorizationAttributeSourceAdvisor;
}
@Bean
public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
daap.setProxyTargetClass(true);
return daap;
}
/**
* 憑證匹配器
* (由于我們的密碼校驗(yàn)交給Shiro的SimpleAuthenticationInfo進(jìn)行處理了
* )
*
* @return
*/
@Bean
public HashedCredentialsMatcher hashedCredentialsMatcher() {
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:這里使用MD5算法;
hashedCredentialsMatcher.setHashIterations(1);//散列的次數(shù),比如散列兩次,相當(dāng)于 md5(md5(""));
return hashedCredentialsMatcher;
}
/**
* cookie對象;
* rememberMeCookie()方法是設(shè)置Cookie的生成模版,比如cookie的name,cookie的有效時間等等。
* @return
*/
/*@Bean
public SimpleCookie rememberMeCookie(){
//這個參數(shù)是cookie的名稱,對應(yīng)前端的checkbox的name = rememberMe
SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
//如果httyOnly設(shè)置為true,則客戶端不會暴露給客戶端腳本代碼,使用HttpOnly cookie有助于減少某些類型的跨站點(diǎn)腳本攻擊;
simpleCookie.setHttpOnly(true);
//記住我cookie生效時間,默認(rèn)30天 ,單位秒:60 * 60 * 24 * 30
//<!-- 記住我cookie生效時間30天 ,單位秒;-->
simpleCookie.setMaxAge(1800);
return simpleCookie;
}*/
/**
* cookie管理對象;
* rememberMeManager()方法是生成rememberMe管理器,而且要將這個rememberMe管理器設(shè)置到securityManager中
* @return
*/
/*@Bean
public CookieRememberMeManager rememberMeManager(){
//System.out.println("ShiroConfiguration.rememberMeManager()");
CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
cookieRememberMeManager.setCookie(rememberMeCookie());
//rememberMe cookie加密的密鑰 建議每個項(xiàng)目都不一樣 默認(rèn)AES算法 密鑰長度(128 256 512 位)
cookieRememberMeManager.setCipherKey(Base64.decode("2AvVhdsgUs0FSA3SDFAdag=="));
return cookieRememberMeManager;
}*/
/**
* shiro session的管理
*/
/*@Bean
public DefaultWebSessionManager sessionManager() {
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
sessionManager.setGlobalSessionTimeout(tomcatTimeout*1000);
//設(shè)置sessionDao對session查詢,在查詢在線用戶service中用到了
sessionManager.setSessionDAO(sessionDAO());
//配置session的監(jiān)聽
Collection<SessionListener> listeners = new ArrayList<SessionListener>();
listeners.add(new BDSessionListener());
sessionManager.setSessionListeners(listeners);
//設(shè)置在cookie中的sessionId名稱
sessionManager.setSessionIdCookie(simpleCookie());
return sessionManager;
}*/
@Bean
@DependsOn("lifecycleBeanPostProcessor")
public EhCacheManager ehCacheManager(CacheManager cacheManager) {
EhCacheManager em = new EhCacheManager();
//將ehcacheManager轉(zhuǎn)換成shiro包裝后的ehcacheManager對象
em.setCacheManager(cacheManager);
em.setCacheManagerConfigFile("classpath:ehcache.xml");
return em;
}
}
到此這篇關(guān)于Java 實(shí)戰(zhàn)項(xiàng)目錘煉之在線購書商城系統(tǒng)的實(shí)現(xiàn)流程的文章就介紹到這了,更多相關(guān)Java 在線購書商城系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析Java中PriorityQueue優(yōu)先級隊列結(jié)構(gòu)的源碼及用法
優(yōu)先級隊列是一種隊列結(jié)構(gòu),是0個或多個元素的集合,每個元素都有一個優(yōu)先權(quán),PriorityQueue被內(nèi)置于JDK中,本文就來解析Java中PriorityQueue優(yōu)先級隊列結(jié)構(gòu)的源碼及用法.2016-05-05
Java實(shí)現(xiàn)微信公眾號發(fā)送模版消息
大家好,本篇文章主要講的是Java實(shí)現(xiàn)微信公眾號發(fā)送模版消息,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
Java使用OCR技術(shù)識別驗(yàn)證碼實(shí)現(xiàn)自動化登陸方法
在本篇文章里小編給大家分享的是關(guān)于Java 如何使用 OCR 技術(shù)識別驗(yàn)證碼實(shí)現(xiàn)自動化登陸的相關(guān)知識點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-08-08
使用Java橋接模式打破繼承束縛優(yōu)雅實(shí)現(xiàn)多維度變化
這篇文章主要為大家介紹了使用Java橋接模式打破繼承束縛,優(yōu)雅實(shí)現(xiàn)多維度變化,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
java實(shí)現(xiàn)水果超市管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)水果超市管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
使用eclipse + maven一步步搭建SSM框架教程詳解
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三個開源框架整合而成,常作為數(shù)據(jù)源較簡單的web項(xiàng)目的框架.這篇文章主要介紹了eclipse + maven搭建SSM框架 ,需要的朋友可以參考下2017-11-11

