提交 78a6418c 编写于 作者: U u014427391

add velocity,luncene,ssi support 20170324 modify by nicky

上级 9acdd910
package net.myblog.biz;
import java.math.BigInteger;
import net.myblog.utils.Tools;
/**
* 权限计算帮助类
*/
public class RightsHelper {
/**
* 利用BigInteger对权限进行2的权的和计算
* @param rights int型权限编码数组
* @return 2的权的和
*/
public static BigInteger sumRights(int[] rights){
BigInteger num = new BigInteger("0");
for(int i=0; i<rights.length; i++){
num = num.setBit(rights[i]);
}
return num;
}
/**
* 利用BigInteger对权限进行2的权的和计算
* @param rights String型权限编码数组
* @return 2的权的和
*/
public static BigInteger sumRights(String[] rights){
BigInteger num = new BigInteger("0");
for(int i=0; i<rights.length; i++){
num = num.setBit(Integer.parseInt(rights[i]));
}
return num;
}
/**
* 测试是否具有指定编码的权限
* @param sum
* @param targetRights
* @return
*/
public static boolean testRights(BigInteger sum,int targetRights){
return sum.testBit(targetRights);
}
/**
* 测试是否具有指定编码的权限
* @param sum
* @param targetRights
* @return
*/
public static boolean testRights(String sum,int targetRights){
if(Tools.isEmpty(sum))
return false;
return testRights(new BigInteger(sum),targetRights);
}
/**
* 测试是否具有指定编码的权限
* @param sum
* @param targetRights
* @return
*/
public static boolean testRights(String sum,String targetRights){
if(Tools.isEmpty(sum))
return false;
return testRights(new BigInteger(sum),targetRights);
}
/**
* 测试是否具有指定编码的权限
* @param sum
* @param targetRights
* @return
*/
public static boolean testRights(BigInteger sum,String targetRights){
return testRights(sum,Integer.parseInt(targetRights));
}
}
package net.myblog.biz.aop;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class BlogManager {
}
package net.myblog.biz.aop;
public class IndexManager {
}
......@@ -6,10 +6,21 @@ package net.myblog.core;
* @date 2017年3月6日
*/
public class Constants {
//验证码Session
public static final String SESSION_SECURITY_CODE = "sessionSecCode";
//用户信息Session
public static final String SESSION_USER = "sessionUser";
//角色权限Session
public static final String SESSION_ROLE_RIGHTS = "sessionRoleRights";
//所有菜单Session
public static final String SESSION_ALLMENU = "sessionAllMenu";
//菜单列表Session
public static final String SESSION_MENULIST = "sessionMenuList";
//权限Session
public static final String SESSION_RIGHTS = "sessionRights";
//页面数量
public static final Integer PAGE_SIZE = 3;
//登录URL
public static final String URL_LOGIN = "/login.do";
......
package net.myblog.core.lucene;
import org.springframework.dao.DataAccessException;
/**
* @description 自定义的Lucene异常类
* @author Nicky
* @date 2017年3月22日
*/
public class LuceneException extends DataAccessException{
/**
*
*/
private static final long serialVersionUID = 1L;
public LuceneException(String message) {
super(message);
}
public LuceneException(String message, Throwable t){
super(message,t);
}
}
package net.myblog.core.lucene;
public class LuceneSearchEngine {
}
......@@ -65,8 +65,8 @@ public class ShiroRealm extends AuthorizingRealm {
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
String username = (String)pc.getPrimaryPrincipal();
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
authorizationInfo.setRoles(userService.getRoles(username));
authorizationInfo.setStringPermissions(userService.getPermissions(username));
// authorizationInfo.setRoles(userService.getRoles(username));
// authorizationInfo.setStringPermissions(userService.getPermissions(username));
System.out.println("Shiro授权");
return authorizationInfo;
}
......
package net.myblog.core.velocity;
import org.springframework.dao.DataAccessException;
/**
* @description 自定义异常类,继承Spring框架的DataAccessException
* @author Nicky
* @date 2017年3月23日
*/
public class VelocityException extends DataAccessException{
/**
*
*/
private static final long serialVersionUID = 1L;
public VelocityException(String msg){
super(msg);
}
public VelocityException(String msg, Throwable cause) {
super(msg, cause);
}
}
package net.myblog.core.velocity;
import java.io.BufferedWriter;
import java.io.File;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import net.myblog.entity.Article;
import net.myblog.utils.DateUtils;
import net.myblog.utils.FileUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* @description
* @author Nicky
* @date 2017年3月24日
*/
public class VelocityManager {
/**
* 加载文章的模板页面
*/
public static void doLoadActiceVM(){
}
/**
* 创建静态模板页面
* @param article
* @param text
* @param savePath
* @throws Exception
*/
public static void doCreateArticleVM(Article article, String text, File savePath)
throws Exception
{
doInit();
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
VelocityContext context = new VelocityContext();
context.put("basePath", request.getContextPath());
context.put("article", article);
context.put("typeId", article.getTypeId());
context.put("text", text);
context.put("createTime", DateUtils.formatDate(article.getArticleTime()));
BufferedWriter writer = FileUtils.fileInit(savePath);
Template template = Velocity.getTemplate("article.vm");
template.merge(context, writer);
}
/**
* 初始化操作,配置日志文件位置,静态页面存放位置,输出输入编码
*/
public static void doInit(){
Properties prop = new Properties();
prop.put("runtime.log","/WEB-INF/log/velocity.log");
prop.put("file.resource.loader.path","/WEB-INF/vm");
prop.put("input.encoding","UTF-8");
prop.put("output.encoding","UTF-8");
try {
Velocity.init(prop);
} catch (Exception e) {
e.printStackTrace();
}
}
}
......@@ -16,54 +16,37 @@ import javax.persistence.TemporalType;
@Entity
public class Article {
/**
* 文章Id,自增
*/
/** 文章Id,自增**/
private int articleId;
/**
* 文章名称
*/
/** 文章名称**/
private String articleName;
/**
* 文章发布时间
*/
/** 文章发布时间**/
private Date articleTime;
/**
* 文章内容
*/
/** 图片路径,测试**/
private String imgPath;
/** 文章内容**/
private String articleContent;
/**
* 查看人数
*/
/** 查看人数**/
private int articleClick;
/**
* 是否博主推荐。0为否;1为是
*/
/** 是否博主推荐。0为否;1为是**/
private int articleSupport;
/**
* 是否置顶。0为;1为是
*/
/** 是否置顶。0为;1为是**/
private int articleUp;
/**
* 文章类别。0为私有,1为公开,2为仅好友查看
*/
/** 文章类别。0为私有,1为公开,2为仅好友查看**/
private int articleType;
/**
* 栏目Id
*/
/** 栏目Id**/
private int typeId;
/**
* 博主Id
*/
/** 博主Id**/
private int userId;
@GeneratedValue
......@@ -95,6 +78,15 @@ public class Article {
this.articleTime = articleTime;
}
@Column(length=100)
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
@Column(nullable=false)
public String getArticleContent() {
return articleContent;
......
package net.myblog.entity;
import java.io.Serializable;
import java.lang.String;
import javax.persistence.*;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
/**
* Entity implementation class for Entity: Menu
*
* @description 菜单信息实体
* @author Nicky
* @date 2017年3月17日
*/
@Table(name="sys_menu")
@Entity
public class Menu implements Serializable {
/**
* 菜单Id
*/
private int menuId;
private int identify;
/**
* 菜单名称
*/
private String name;
/**
* 菜单图标
*/
private String menuIcon;
/**
* 菜单URL
*/
private String menuUrl;
/**
* 菜单类型
*/
private String menuType;
private Menu parentMenu;
private List<Menu> subMenu;
private boolean hasMenu = false;
private static final long serialVersionUID = 1L;
public Menu() {
......@@ -35,14 +67,7 @@ public class Menu implements Serializable {
this.menuId = menuId;
}
@Column(unique=true)
public int getIdentify() {
return this.identify;
}
public void setIdentify(int identify) {
this.identify = identify;
}
@Column(length=100)
public String getName() {
return this.name;
}
......@@ -50,6 +75,8 @@ public class Menu implements Serializable {
public void setName(String name) {
this.name = name;
}
@Column(length=30)
public String getMenuIcon() {
return this.menuIcon;
}
......@@ -57,6 +84,8 @@ public class Menu implements Serializable {
public void setMenuIcon(String menuIcon) {
this.menuIcon = menuIcon;
}
@Column(length=100)
public String getMenuUrl() {
return this.menuUrl;
}
......@@ -64,6 +93,8 @@ public class Menu implements Serializable {
public void setMenuUrl(String menuUrl) {
this.menuUrl = menuUrl;
}
@Column(length=100)
public String getMenuType() {
return this.menuType;
}
......@@ -71,5 +102,32 @@ public class Menu implements Serializable {
public void setMenuType(String menuType) {
this.menuType = menuType;
}
@Transient
public Menu getParentMenu() {
return parentMenu;
}
public void setParentMenu(Menu parentMenu) {
this.parentMenu = parentMenu;
}
@Transient
public List<Menu> getSubMenu() {
return subMenu;
}
public void setSubMenu(List<Menu> subMenu) {
this.subMenu = subMenu;
}
@Transient
public boolean isHasMenu() {
return hasMenu;
}
public void setHasMenu(boolean hasMenu) {
this.hasMenu = hasMenu;
}
}
......@@ -25,7 +25,6 @@ import javax.persistence.Table;
@Table(name="sys_permission")
@Entity
public class Permission implements Serializable {
private int id;
private String desc;
......
......@@ -41,9 +41,9 @@ public class Role implements Serializable{
private String name;
/**
* 角色标志
* 权限
*/
private String role;
private String rights;
private Set<Permission> permissions = new HashSet<Permission>();
......@@ -75,13 +75,13 @@ public class Role implements Serializable{
this.name = name;
}
@Column(unique=true,length=100)
public String getRole() {
return role;
@Column(length=100)
public String getRights() {
return rights;
}
public void setRole(String role) {
this.role = role;
public void setRights(String rights) {
this.rights = rights;
}
@OneToMany(targetEntity=Permission.class,cascade=CascadeType.MERGE,fetch=FetchType.EAGER)
......
......@@ -14,19 +14,13 @@ import javax.persistence.Table;
@Entity
public class SecretMessage {
/**
* 自增Id
*/
/** 自增Id **/
private int secretId;
/**
* 发送者Id
*/
/** 发送者Id **/
private int sendId;
/**
* 接收者Id
*/
/** 接收者Id **/
private int receiveId;
/**
......
package net.myblog.entity;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
......@@ -91,7 +89,12 @@ public class User {
*/
private Boolean locked = Boolean.FALSE;
private Set<Role> roles = new HashSet<Role>();
/**
* 权限
*/
private String rights;
private Role role;
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Id
......@@ -210,16 +213,22 @@ public class User {
this.locked = locked;
}
//角色与用户是多对多的关联
@ManyToMany(targetEntity = Role.class, cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
@JoinTable(name = "sys_user_role", joinColumns = @JoinColumn(name = "userId", referencedColumnName = "id") , inverseJoinColumns = @JoinColumn(name = "roleId", referencedColumnName = "roleId") )
public Set<Role> getRoles() {
return roles;
public String getRights() {
return rights;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
public void setRights(String rights) {
this.rights = rights;
}
// @ManyToMany(targetEntity = Role.class, cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
// @JoinTable(name = "sys_user_role", joinColumns = @JoinColumn(name = "userId", referencedColumnName = "id") , inverseJoinColumns = @JoinColumn(name = "roleId", referencedColumnName = "roleId") )
// public Role getRole() {
// return role;
// }
//
// public void setRole(Role role) {
// this.role = role;
// }
}
......@@ -14,24 +14,16 @@ import javax.persistence.Table;
@Entity
public class WebAd {
/**
* 网站广告Id
*/
/** 网站广告Id **/
private int adId;
/**
* 广告标题
*/
/** 广告标题 **/
private String adTitle;
/**
* 广告图片路径
*/
/** 广告图片路径 **/
private String adImage;
/**
* 广告描述
*/
/** 广告描述 **/
private String adDesc;
@GeneratedValue
......
package net.myblog.repository;
import net.myblog.entity.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface ArticleRepository extends PagingAndSortingRepository<Article,Integer>{
}
package net.myblog.repository;
import java.util.List;
import java.util.Set;
import net.myblog.entity.Menu;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MenuRepository extends JpaRepository<Menu, Integer>{
public List<Menu> findAll();
}
package net.myblog.service;
import net.myblog.entity.Article;
import net.myblog.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
@Service
public class ArticleService {
@Autowired ArticleRepository articleRepository;
/**
* 获取所有的博客信息并分页显示
* @param pageNo
* 当前页面数
* @param pageSize
* 每一页面的页数
* @return
*/
public Page<Article> findAll(int pageNo, int pageSize){
PageRequest request = buildPageRequest(pageNo, pageSize);
Page<Article> articles = articleRepository.findAll(request);
return articles;
}
/**
* 构建PageRequest
* @param pageNo
* 当前页数
* @param pageSize
* 每一页面的页数
* @return
*/
public PageRequest buildPageRequest(int pageNo, int pageSize){
return new PageRequest(pageNo-1, pageSize,null);
}
}
......@@ -7,6 +7,7 @@ import net.myblog.repository.ArticleSortRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ArticleSortService {
......@@ -17,6 +18,7 @@ public class ArticleSortService {
* 获取所有的博客标签(类别)信息
* @return
*/
@Transactional(readOnly=true)
public List<ArticleSort> findAll(){
return articleSortRepository.findAll();
}
......
......@@ -7,6 +7,7 @@ import net.myblog.repository.FriendlyLinkRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class FriendlyLinkService {
......@@ -18,6 +19,7 @@ public class FriendlyLinkService {
* 获取所有的友情链接信息
* @return
*/
@Transactional
public List<FriendlyLink> findAll(){
return friendlyLinkRepository.findAll();
}
......
package net.myblog.service;
import java.util.List;
import net.myblog.entity.Menu;
import net.myblog.repository.MenuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class MenuService {
@Autowired MenuRepository menuRepository;
/**
* 查询所有的菜单
* @return
*/
@Transactional
public List<Menu> findAll(){
return menuRepository.findAll();
}
}
......@@ -38,35 +38,35 @@ public class UserService {
* @param username
* @return
*/
public Set<String> getRoles(String username){
User user = userRepository.findByUsername(username);
Set<Role> roles = user.getRoles();
//创建一个HashSet来存放用户角色信息
Set<String> roleStrs = new HashSet<String>();
for(Role r:roles){
roleStrs.add(r.getRole());
}
return roleStrs;
}
// public Set<String> getRoles(String username){
// User user = userRepository.findByUsername(username);
// Set<Role> roles = user.getRoles();
// //创建一个HashSet来存放用户角色信息
// Set<String> roleStrs = new HashSet<String>();
// for(Role r:roles){
// roleStrs.add(r.getRole());
// }
// return roleStrs;
// }
/**
*
* @param username
* @return
*/
public Set<String> getPermissions(String username){
User user = userRepository.findByUsername(username);
Set<Role> roles = user.getRoles();
//创建一个HashSet来存放角色权限信息
Set<String> permissionStrs = new HashSet<String>();
for(Role r:roles){
for(Permission p:r.getPermissions())
for(Operation ope:p.getOperations()){
permissionStrs.add(ope.getOperation());
}
}
return permissionStrs;
}
// public Set<String> getPermissions(String username){
// User user = userRepository.findByUsername(username);
// Set<Role> roles = user.getRoles();
// //创建一个HashSet来存放角色权限信息
// Set<String> permissionStrs = new HashSet<String>();
// for(Role r:roles){
// for(Permission p:r.getPermissions())
// for(Operation ope:p.getOperations()){
// permissionStrs.add(ope.getOperation());
// }
// }
// return permissionStrs;
// }
@Transactional(readOnly=true)
public User findByUsername(String username){
......
......@@ -7,6 +7,7 @@ import net.myblog.repository.WebAdRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class WebAdService {
......@@ -17,6 +18,7 @@ public class WebAdService {
* 查询所有的广告信息
* @return
*/
@Transactional(readOnly=true)
public List<WebAd> findAll(){
return webAdRepository.findAll();
}
......
package net.myblog.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
public static String formatDate(Date date) throws ParseException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date);
}
}
package net.myblog.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class FileUtils {
public static BufferedWriter fileInit(File file){
BufferedWriter bw = null;
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try{
if(!file.getParentFile().exists()){
file.getParentFile().mkdir();
}
if(!file.exists()){
file.createNewFile();
}
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
}catch(IOException e){
throw new RuntimeException("create file error...");
}finally{
try {
fos.close();
osw.close();
bw.close();
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
return bw;
}
}
package net.myblog.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @description 项目的工具类
* @author Nicky
......
......@@ -20,7 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping(name="/articleSort")
public class ArticleSortController extends BaseController{
@Resource ArticleSortService articleSortService;
@Autowired ArticleSortService articleSortService;
/**
* 获取所有的博客标签(分类)信息
......
package net.myblog.web.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.myblog.core.Constants;
import net.myblog.entity.Article;
import net.myblog.entity.ArticleSort;
import net.myblog.entity.FriendlyLink;
import net.myblog.entity.WebAd;
import net.myblog.service.ArticleService;
import net.myblog.service.ArticleSortService;
import net.myblog.service.FriendlyLinkService;
import net.myblog.service.WebAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class BlogIndexController extends BaseController{
@Autowired
ArticleService articleService;
@Autowired
ArticleSortService articleSortService;
@Autowired
FriendlyLinkService friendlyLinkService;
@Autowired
WebAdService webAdService;
/**
* 访问博客主页
* @return
*/
@RequestMapping(value="/toblog",produces="text/html;charset=UTF-8")
public ModelAndView toBlog(HttpServletRequest request, HttpServletResponse response, Model model)throws ClassNotFoundException{
ModelAndView mv = this.getModelAndView();
String pageNoString = request.getParameter("pageNo");
if(pageNoString==null||"".equals(pageNoString)){
pageNoString = "1";
}
int pageNo = Integer.parseInt(pageNoString);
int pageSize = Constants.PAGE_SIZE;
Page<Article> articlePage = articleService.findAll(pageNo, pageSize);
List<ArticleSort> articleSorts = articleSortService.findAll();
List<FriendlyLink> links = friendlyLinkService.findAll();
List<WebAd> webAds = webAdService.findAll();
model.addAttribute("articles", articlePage.getContent());
model.addAttribute("pageSize", pageSize);
model.addAttribute("totalPage",articlePage.getTotalElements());
model.addAttribute("articleSorts", articleSorts);
model.addAttribute("links",links);
model.addAttribute("webAds", webAds);
mv.setViewName("myblog/index");
return mv;
}
}
......@@ -4,6 +4,7 @@ import javax.annotation.Resource;
import net.myblog.service.FriendlyLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -11,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping(name="/link")
public class FriendlyLinkController extends BaseController{
@Resource
@Autowired
FriendlyLinkService friendlyLinkService;
......
package net.myblog.web.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -7,13 +8,17 @@ import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import net.myblog.biz.RightsHelper;
import net.myblog.core.Constants;
import net.myblog.entity.ArticleSort;
import net.myblog.entity.FriendlyLink;
import net.myblog.entity.Menu;
import net.myblog.entity.Role;
import net.myblog.entity.User;
import net.myblog.entity.WebAd;
import net.myblog.service.ArticleSortService;
import net.myblog.service.FriendlyLinkService;
import net.myblog.service.MenuService;
import net.myblog.service.UserService;
import net.myblog.service.WebAdService;
import net.myblog.utils.Tools;
......@@ -25,6 +30,7 @@ import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -40,14 +46,10 @@ import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController extends BaseController{
@Resource
@Autowired
UserService userService;
@Resource
ArticleSortService articleSortService;
@Resource
FriendlyLinkService friendlyLinkService;
@Resource
WebAdService webAdService;
@Autowired
MenuService menuService;
/**
* 获取登录用户的IP
......@@ -67,23 +69,6 @@ public class LoginController extends BaseController{
userService.saveIP(map);
}
/**
* 访问博客主页
* @return
*/
@RequestMapping(value="/toblog",produces="text/html;charset=UTF-8")
public ModelAndView toBlog(Model model)throws ClassNotFoundException{
ModelAndView mv = this.getModelAndView();
List<ArticleSort> articleSorts = articleSortService.findAll();
List<FriendlyLink> links = friendlyLinkService.findAll();
List<WebAd> webAds = webAdService.findAll();
model.addAttribute("articleSorts", articleSorts);
model.addAttribute("links",links);
model.addAttribute("webAds", webAds);
mv.setViewName("myblog/index");
return mv;
}
/**
* 访问后台登录页面
* @return
......@@ -111,8 +96,8 @@ public class LoginController extends BaseController{
String logindata[] = request.getParameter("LOGINDATA").split(",");
if(logindata != null && logindata.length == 3){
//获取Shiro管理的Session
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
Subject subject = SecurityUtils.getSubject();
Session session = subject.getSession();
String codeSession = (String)session.getAttribute(Constants.SESSION_SECURITY_CODE);
String code = logindata[2];
/**检测页面验证码是否为空,调用工具类检测**/
......@@ -138,9 +123,9 @@ public class LoginController extends BaseController{
//保存登录IP
getRemortIP(username);
/**Shiro加入身份验证**/
Subject subject = SecurityUtils.getSubject();
Subject sub = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username,password);
subject.login(token);
sub.login(token);
}
}else{
//账号或者密码错误
......@@ -167,14 +152,52 @@ public class LoginController extends BaseController{
@RequestMapping(value="/admin/index")
public ModelAndView toMain() throws AuthenticationException{
ModelAndView mv = this.getModelAndView();
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
Subject subject = SecurityUtils.getSubject();
Session session = subject.getSession();
User user = (User)session.getAttribute(Constants.SESSION_USER);
if(user != null){
Role role = null;
String rights = role != null?role.getRights():"";
session.setAttribute(Constants.SESSION_RIGHTS, rights);
List<Menu> allmenu= new ArrayList<Menu>();
if(session.getAttribute(Constants.SESSION_ALLMENU) == null){
allmenu = menuService.findAll();
for(Menu m:allmenu){
m.setHasMenu(RightsHelper.testRights(rights, m.getMenuId()));
if(m.isHasMenu()){
List<Menu> subMenuList = m.getSubMenu();
for(Menu submenu:subMenuList){
submenu.setHasMenu(RightsHelper.testRights(rights, submenu.getMenuId()));
}
}
}
session.setAttribute(Constants.SESSION_ALLMENU, allmenu);
}else{
allmenu = (List<Menu>)session.getAttribute(Constants.SESSION_ALLMENU);
}
List<Menu> menuList = new ArrayList<Menu>();
if(session.getAttribute(Constants.SESSION_MENULIST)==null){
List<Menu> menuList1 = new ArrayList<Menu>();
List<Menu> menuList2 = new ArrayList<Menu>();
for(Menu mu:allmenu){
if(mu.getMenuId()==1){
menuList1.add(mu);
}else{
menuList2.add(mu);
}
}
}
mv.addObject("menus",allmenu);
}else{
//会话失效,返回登录界面
//mv.setViewName("admin/login");
mv.setViewName("admin/login");
}
mv.setViewName("admin/index");
return mv;
......@@ -188,10 +211,12 @@ public class LoginController extends BaseController{
public ModelAndView logout(){
ModelAndView mv = this.getModelAndView();
/**Shiro管理Session**/
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
Subject sub = SecurityUtils.getSubject();
Session session = sub.getSession();
session.removeAttribute(Constants.SESSION_USER);
session.removeAttribute(Constants.SESSION_SECURITY_CODE);
session.removeAttribute(Constants.SESSION_ALLMENU);
session.removeAttribute(Constants.SESSION_MENULIST);
/**Shiro销毁登录**/
Subject subject = SecurityUtils.getSubject();
subject.logout();
......
......@@ -13,6 +13,7 @@ import net.myblog.service.ArticleSortService;
import net.myblog.service.FriendlyLinkService;
import net.myblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -28,7 +29,7 @@ import org.springframework.web.servlet.ModelAndView;
@RequestMapping(value="/user")
public class UserController extends BaseController{
@Resource
@Autowired
UserService userService;
}
......@@ -20,6 +20,14 @@
<!-- 启用Spring框架的注解 -->
<context:annotation-config />
<!-- 引入Shiro框架配置 start -->
<import resource="spring-shiro.xml" />
<!-- 引入Shiro框架配置 end -->
<!-- 引入Velocity的配置 start -->
<import resource="spring-velocity.xml"/>
<!-- 引入Velocity的配置 end -->
<!-- 启动注解扫描,组件由SpringMVC配置文件扫描 -->
<context:component-scan base-package="net.myblog" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
......@@ -112,7 +120,10 @@
<tx:method name="save*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
</tx:advice>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 开启Spring AOP的织入代理 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
......@@ -123,41 +134,5 @@
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
</aop:config>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Shiro start -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="ShiroRealm" />
</bean>
<!-- 項目自定义的Realm -->
<bean id="ShiroRealm" class="net.myblog.core.security.ShiroRealm" ></bean>
<!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/admin/index" />
<property name="unauthorizedUrl" value="/login" />
<property name="filterChainDefinitions">
<value>
/static/** = anon
/upload/** = anon
/plugins/** = anon
/code.do = anon
/login.do = anon
/logincheck.do = anon
/toblog.do = anon
/** = authc
</value>
</property>
</bean>
<!-- Shiro end -->
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- Shiro start -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="ShiroRealm" />
</bean>
<!-- 項目自定义的Realm -->
<bean id="ShiroRealm" class="net.myblog.core.security.ShiroRealm" ></bean>
<!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/admin/index" />
<property name="unauthorizedUrl" value="/login" />
<property name="filterChainDefinitions">
<value>
/static/** = anon
/upload/** = anon
/plugins/** = anon
/code.do = anon
/login.do = anon
/logincheck.do = anon
/toblog.do = anon
/** = authc
</value>
</property>
</bean>
<!-- Shiro end -->
</beans>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- Velocity视图解析器配置 start-->
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="velocity" />
<property name="suffix" value=".vm" />
</bean>
<!-- Velocity视图解析器配置 end -->
<!-- Velocity环境配置 start-->
<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<!-- velocity配置文件路径 -->
<property name="configLocation" value="classpath:velocity.properties"/>
<!-- velocity模板路径 -->
<property name="resourceLoaderPath" value="/WEB-INF/vm/"/>
</bean>
<!-- Velocity环境配置 end -->
</beans>
\ No newline at end of file
runtime.log:/WEB-INF/log/velocity.log
file.resource.loader.path:/WEB-INF/vm
input.encoding:UTF-8
output.encoding:UTF-8
......@@ -139,6 +139,7 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
tabPanel.tabs('select',title);
}
}
/**
* Name 移除菜单选项
*/
......
......@@ -3,6 +3,7 @@
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String loadBlogUrl = basePath + "toblog.do?pageNo=";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
......@@ -16,7 +17,9 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
<meta name="description" content="" />
<link href="static/css/base.css" rel="stylesheet">
<link href="static/css/index.css" rel="stylesheet">
<script type="text/javascript" src="static/js/jquery.min.js"></script>
<link href="static/css/pageBase.css" rel="stylesheet">
<link href="static/css/pageGroup.css" rel="stylesheet">
<script type="text/javascript" src="static/js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="static/js/sliders.js"></script>
<!--[if lt IE 9]>
<script src="js/modernizr.js"></script>
......@@ -25,6 +28,144 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
<script type="text/javascript" src="static/js/up/jquery.js"></script>
<script type="text/javascript" src="static/js/up/js.js"></script>
<!-- 返回顶部调用 end-->
<script type="text/javascript">
$(document).ready(function(){
var totalPageGet = ${totalPage};
var totalPage = Number(totalPageGet);
var pageSizeGet = ${pageSize};
var pageSize = Number(pageSizeGet);
var pageCount = totalPage/pageSize;
});
</script>
<script type="text/javascript">
$(function(){
//根据总页数判断,如果小于5页,则显示所有页数,如果大于5页,则显示5页。根据当前点击的页数生成
var totalPageGet = ${totalPage};
var totalPage = Number(totalPageGet);
var pageSizeGet = ${pageSize};
var pageSize = Number(pageSizeGet);
var pageCount = totalPage/pageSize;//模拟后台总页数
//生成分页按钮
if(pageCount>5){
page_icon(1,5,0);
}else{
page_icon(1,pageCount,0);
}
//点击分页按钮触发
$("#pageGro li").live("click",function(){
if(pageCount > 5){
var pageNum = parseInt($(this).html());//获取当前页数
pageGroup(pageNum,pageCount);
}else{
$(this).addClass("on");
$(this).siblings("li").removeClass("on");
}
});
//点击上一页触发
$("#pageGro .pageUp").click(function(){
if(pageCount > 5){
var pageNum = parseInt($("#pageGro li.on").html());//获取当前页
pageUp(pageNum,pageCount);
}else{
var index = $("#pageGro ul li.on").index();//获取当前页
if(index > 0){
$("#pageGro li").removeClass("on");//清除所有选中
$("#pageGro ul li").eq(index-1).addClass("on");//选中上一页
}
}
});
//点击下一页触发
$("#pageGro .pageDown").click(function(){
if(pageCount > 5){
var pageNum = parseInt($("#pageGro li.on").html());//获取当前页
pageDown(pageNum,pageCount);
}else{
var index = $("#pageGro ul li.on").index();//获取当前页
if(index+1 < pageCount){
$("#pageGro li").removeClass("on");//清除所有选中
$("#pageGro ul li").eq(index+1).addClass("on");//选中上一页
}
}
});
});
//点击跳转页面
function pageGroup(pageNum,pageCount){
switch(pageNum){
case 1:
page_icon(1,5,0);
break;
case 2:
page_icon(1,5,1);
break;
case pageCount-1:
page_icon(pageCount-4,pageCount,3);
break;
case pageCount:
page_icon(pageCount-4,pageCount,4);
break;
default:
page_icon(pageNum-2,pageNum+2,2);
break;
}
}
//根据当前选中页生成页面点击按钮
function page_icon(page,count,eq){
var html = "";
for(var i = 0;i<=count;i++){
var url = "<li><a href=\"<%=loadBlogUrl%>"+(i+1)+"\">"+(i+1)+"</a></li>";
html += url;
}
$("#pageGro ul").html(html);
$("#pageGro ul li").eq(eq).addClass("on");
}
//上一页
function pageUp(pageNum,pageCount){
switch(pageNum){
case 1:
break;
case 2:
page_icon(1,5,0);
break;
case pageCount-1:
page_icon(pageCount-4,pageCount,2);
break;
case pageCount:
page_icon(pageCount-4,pageCount,3);
break;
default:
page_icon(pageNum-2,pageNum+2,1);
break;
}
}
//下一页
function pageDown(pageNum,pageCount){
switch(pageNum){
case 1:
page_icon(1,5,1);
break;
case 2:
page_icon(1,5,2);
break;
case pageCount-1:
page_icon(pageCount-4,pageCount,4);
break;
case pageCount:
break;
default:
page_icon(pageNum-2,pageNum+2,3);
break;
}
}
</script>
</head>
<body>
<header>
......@@ -99,64 +240,43 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
<!-- banner代码 结束 -->
<div class="topnews">
<h2><span><a href="/" target="_blank">栏目标题</a><a href="/" target="_blank">栏目标题</a><a href="/" target="_blank">栏目标题</a></span><b>文章</b>推荐</h2>
<div class="blogs">
<figure><img src="static/images/01.jpg"></figure>
<ul>
<h3><a href="/">住在手机里的朋友</a></h3>
<p>通信时代,无论是初次相见还是老友重逢,交换联系方式,常常是彼此交换名片,然后郑重或是出于礼貌用手机记下对方的电话号码。在快节奏的生活里,我们不知不觉中就成为住在别人手机里的朋友。又因某些意外,变成了别人手机里匆忙的过客,这种快餐式的友谊 ...</p>
<p class="autor"><span class="lm f_l"><a href="/">个人博客</a></span><span class="dtime f_l">2014-02-19</span><span class="viewnum f_r">浏览(<a href="/">459</a></span><span class="pingl f_r">评论(<a href="/">30</a></span></p>
</ul>
</div>
<div class="blogs">
<figure><img src="static/images/02.jpg"></figure>
<ul>
<h3><a href="/">教你怎样用欠费手机拨打电话</a></h3>
<p>初次相识的喜悦,让你觉得似乎找到了知音。于是,对于投缘的人,开始了较频繁的交往。渐渐地,初识的喜悦退尽,接下来就是仅仅保持着联系,平淡到偶尔在节假曰发短信互致问候...</p>
<p class="autor"><span class="lm f_l"><a href="/">个人博客</a></span><span class="dtime f_l">2014-02-19</span><span class="viewnum f_r">浏览(<a href="/">459</a></span><span class="pingl f_r">评论(<a href="/">30</a></span></p>
</ul>
</div>
<h2><b>文章</b>推荐</h2>
<c:choose>
<c:when test="${not empty articles}">
<c:forEach items="${articles}" var="ar" varStatus="art">
<div class="blogs">
<figure><img src="static/images/03.jpg"></figure>
<figure><img src="${basePath }${ar.imgPath}"></figure>
<ul>
<h3><a href="/">原来以为,一个人的勇敢是,删掉他的手机号码...</a></h3>
<p>原来以为,一个人的勇敢是,删掉他的手机号码、QQ号码等等一切,努力和他保持距离。等着有一天,习惯不想念他,习惯他不在身边,习惯时间把他在我记忆里的身影磨蚀干净... </p>
<p class="autor"><span class="lm f_l"><a href="/">个人博客</a></span><span class="dtime f_l">2014-02-19</span><span class="viewnum f_r">浏览(<a href="/">459</a></span><span class="pingl f_r">评论(<a href="/">30</a></span></p>
<h3><a href="/">${ar.articleName }</a></h3>
<p>${ar.articleContent}</p>
<p class="autor">
<span class="lm f_l"><a href="/">个人博客</a></span>
<span class="dtime f_l">${ar.articleTime}</span>
<span class="viewnum f_r">浏览(<a href="/">${ar.articleClick }</a></span>
<span class="pingl f_r">评论(<a href="/">30</a></span>
</p>
</ul>
</div>
<div class="blogs">
<figure><img src="static/images/04.jpg"></figure>
<ul>
<h3><a href="/">手机的16个惊人小秘密,据说99.999%的人都不知</a></h3>
<p>引导语:知道么,手机有备用电池,手机拨号码12593+电话号码=陷阱……手机具有很多你不知道的小秘密,说出来一定很惊奇!不信的话就来看看吧!...</p>
<p class="autor"><span class="lm f_l"><a href="/">个人博客</a></span><span class="dtime f_l">2014-02-19</span><span class="viewnum f_r">浏览(<a href="/">459</a></span><span class="pingl f_r">评论(<a href="/">30</a></span></p>
</ul>
</div>
<div class="blogs">
<figure><img src="static/images/05.jpg"></figure>
<ul>
<h3><a href="/">你面对的是生活而不是手机</a></h3>
<p>每一次与别人吃饭,总会有人会拿出手机。以为他们在打电话或者有紧急的短信,但用余光瞟了一眼之后发现无非就两件事:1、看小说,2、上人人或者QQ...</p>
<p class="autor"><span class="lm f_l"><a href="/">个人博客</a></span><span class="dtime f_l">2014-02-19</span><span class="viewnum f_r">浏览(<a href="/">459</a></span><span class="pingl f_r">评论(<a href="/">30</a></span></p>
</ul>
</div>
<div class="blogs">
<figure><img src="static/images/06.jpg"></figure>
<ul>
<h3><a href="/">豪雅手机正式发布! 在法国全手工打造的奢侈品</a></h3>
<p>现在跨界联姻,时尚、汽车以及运动品牌联合手机制造商联合发布手机产品在行业里已经不再新鲜,上周我们给大家报道过著名手表制造商瑞士泰格·豪雅(Tag Heuer) 联合法国的手机制造商Modelabs发布的一款奢华手机的部分谍照,而近日该手机终于被正式发布了...</p>
<p class="autor"><span class="lm f_l"><a href="/">个人博客</a></span><span class="dtime f_l">2014-02-19</span><span class="viewnum f_r">浏览(<a href="/">459</a></span><span class="pingl f_r">评论(<a href="/">30</a></span></p>
</ul>
</div>
<div class="blogs">
<figure><img src="static/images/04.jpg"></figure>
<ul>
<h3><a href="/">手机的16个惊人小秘密,据说99.999%的人都不知</a></h3>
<p>引导语:知道么,手机有备用电池,手机拨号码12593+电话号码=陷阱……手机具有很多你不知道的小秘密,说出来一定很惊奇!不信的话就来看看吧!...</p>
<p class="autor"><span class="lm f_l"><a href="/">个人博客</a></span><span class="dtime f_l">2014-02-19</span><span class="viewnum f_r">浏览(<a href="/">459</a></span><span class="pingl f_r">评论(<a href="/">30</a></span></p>
</c:forEach>
</c:when>
<c:otherwise>
<li>没有相关数据</li>
</c:otherwise>
</c:choose>
<div id="pageGro" class="cb">
<div class="pageUp">上一页</div>
<div class="pageList">
<ul id="link">
</ul>
</div>
</div>
<div class="pageDown">下一页</div>
</div>
</div>
</div>
<div class="r_box f_r">
<div class="tit01">
......@@ -175,7 +295,7 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
</div>
<div class="moreSelect" id="lp_right_select">
<script>
<script>
window.onload = function ()
{
var oLi = document.getElementById("tab").getElementsByTagName("li");
......@@ -189,7 +309,7 @@ window.onload = function ()
for(var n = 0; n < oLi.length; n++) oLi[n].className="";
this.className = "cur";
for(var n = 0; n < oUl.length; n++) oUl[n].style.display = "none";
oUl[this.index].style.display = "block"
oUl[this.index].style.display = "block";
}
}
}
......
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div class="blogs">
<figure><img src="static/images/02.jpg"></figure>
<ul>
<h3><a href="/">${article.articleName}</a></h3>
<p>${article.articleContent}</p>
<p class="autor">
<span class="lm f_l"><a href="/">个人博客</a></span>
<span class="dtime f_l">${article.articleTime}</span>
<span class="viewnum f_r">浏览(<a href="/">${article.articleClick}</a></span>
<span class="pingl f_r">评论(<a href="/">30</a></span>
</p>
</ul>
</div>
</body>
</html>
\ No newline at end of file
......@@ -3,6 +3,7 @@
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-base.xml</param-value>
......@@ -11,6 +12,7 @@
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
......@@ -27,7 +29,19 @@
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 连接池 启用Web监控统计功能 -->
<!-- 添加SSI(服务端页面包含技术)配置 start -->
<servlet>
<servlet-name>ssi</servlet-name>
<servlet-class>org.apache.catalina.ssi.SSIServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ssi</servlet-name>
<url-pattern>*.shtml</url-pattern>
</servlet-mapping>
<!-- 添加SSI(服务端页面包含技术)配置 end -->
<!-- 连接池 启用Web监控统计功能 start-->
<filter>
<filter-name>DruidWebStatFilter</filter-name>
<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
......@@ -48,12 +62,15 @@
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>
<!-- 连接池 启用Web监控统计功能 end-->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
......@@ -63,13 +80,12 @@
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Shiro过滤器配置 start -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>
......@@ -84,7 +100,10 @@
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Shiro过滤器配置 end -->
<session-config>
<session-timeout>600</session-timeout>
</session-config>
</web-app>
\ No newline at end of file
此差异已折叠。
/* CSS Document */
/*分页*/
#pageGro{ width:400px; height:25px; margin:0px auto; padding-top:30px;}
#pageGro div,#pageGro div ul li{ font-size:12px; color:#999; line-height:23px; float:left; margin-left:5px;}
#pageGro div ul li{ width:22px; text-align:center; border:1px solid #999; cursor:pointer;}
#pageGro div ul li.on{ color:#fff; background:#3c90d9; border:1px solid #3c90d9;}
#pageGro .pageUp,#pageGro .pageDown{ width:63px; border:1px solid #999; cursor:pointer;}
#pageGro .pageUp{ text-indent:23px; }
#pageGro .pageDown{ text-indent:5px;}
package net.myblog.junit.test;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
public class MyTest {
public static void main(String[] args) throws Exception {
// //初始化参数
// Properties properties=new Properties();
// //设置velocity资源加载方式为file
// //配置Velocity日志文件存放位置
// properties.put("runtime.log", "/WEB-INF/log/velocity.log");
// //配置Velocity模板文件存放位置
// properties.put("file.resource.loader.path", "/WEB-INF/vm");
// //配置输入编码格式
// properties.put("input.encoding", "UTF-8");
// //配置输出编码格式
// properties.put("output.encoding", "UTF-8");
//实例化一个VelocityEngine对象
VelocityEngine velocityEngine=new VelocityEngine();
//实例化一个VelocityContext
VelocityContext context=new VelocityContext();
//向VelocityContext中放入键值
context.put("username", "张三");
context.put("password", "123456789");
context.put("age", "20");
context.put("address", "陕西西安");
context.put("blog", "http://blogjava.net/sxyx2008");
//实例化一个StringWriter
StringWriter writer=new StringWriter();
//从vm目录下加载hello.vm模板,在eclipse工程中该vm目录与src目录平级
velocityEngine.mergeTemplate("vm/article.vm", "UTF-8", context, writer);
System.out.println(writer.toString());
}
}
......@@ -20,6 +20,14 @@
<!-- 启用Spring框架的注解 -->
<context:annotation-config />
<!-- 引入Shiro框架配置 start -->
<import resource="spring-shiro.xml" />
<!-- 引入Shiro框架配置 end -->
<!-- 引入Velocity的配置 start -->
<import resource="spring-velocity.xml"/>
<!-- 引入Velocity的配置 end -->
<!-- 启动注解扫描,组件由SpringMVC配置文件扫描 -->
<context:component-scan base-package="net.myblog" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
......@@ -112,7 +120,10 @@
<tx:method name="save*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
</tx:advice>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 开启Spring AOP的织入代理 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
......@@ -123,41 +134,5 @@
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
</aop:config>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Shiro start -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="ShiroRealm" />
</bean>
<!-- 項目自定义的Realm -->
<bean id="ShiroRealm" class="net.myblog.core.security.ShiroRealm" ></bean>
<!-- Shiro Filter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/admin/index" />
<property name="unauthorizedUrl" value="/login" />
<property name="filterChainDefinitions">
<value>
/static/** = anon
/upload/** = anon
/plugins/** = anon
/code.do = anon
/login.do = anon
/logincheck.do = anon
/toblog.do = anon
/** = authc
</value>
</property>
</bean>
<!-- Shiro end -->
</beans>
这是velocity默认的模板加载方式,从文件中加载模板文件
hellow ${username},
这是你的信息
用户密码为:${password}
年龄为:${age}
出生地址为:${address}
个人主页为:<a href='${blog}'>${blog}</a>
\ No newline at end of file
这是velocity默认的模板加载方式,从文件中加载模板文件
hellow ${username},
这是你的信息
用户密码为:${password}
年龄为:${age}
出生地址为:${address}
个人主页为:<a href='${blog}'>${blog}</a>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册