0005.用户专栏和文章的展示,以及权限过滤

本文介绍了一种基于用户登录状态展示个人专栏及其文章的方法,利用EL表达式和JSTL标签实现数据展示,并通过自定义拦截器确保页面访问的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


需求:用户登录后,能在个人页面看到自己的专栏和专栏下的文章。

实现:因为登录时,已经把登录用户信息放入session中,所以直接在页面使用el表达式取出即可。

专栏实体类Category:

public class Category {
	private Long cate_id;
	private String title;//专栏名称
	private User user;//专栏作者
	private Set<Article> articles = new HashSet<Article>();
	
	
	public Long getCate_id() {
		return cate_id;
	}
	public Set<Article> getArticles() {
		return articles;
	}
	public void setArticles(Set<Article> articles) {
		this.articles = articles;
	}
	public void setCate_id(Long cate_id) {
		this.cate_id = cate_id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
}
文章实体类Article:

public class Article {
	private Long arti_id;
	private String title;//文章标题
	private String content;//内容
	private String status;//文章状态
	private Category category;//专栏分类
	
	public Long getArti_id() {
		return arti_id;
	}
	public void setArti_id(Long arti_id) {
		this.arti_id = arti_id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	
}

专栏实体类Category的映射文件配置:

    <hibernate-mapping package="com.dengtuzi.dtb.domain">
    	<!--  -->
    	<class name="Category" table="dtb_category">
    		<id name="cate_id">
    			<generator class="native"></generator>
    		</id>
    		<property name="title" ></property>
    		
    		<many-to-one  name="user" column="cate_user_id" class="User"></many-to-one>
    		<set name="articles" >
    			<key column="arti_cate_id"></key>
    			<one-to-many class="Article" />
    		</set>
    	</class>
    </hibernate-mapping>
文章实体类Article的映射文件配置:

    <hibernate-mapping package="com.dengtuzi.dtb.domain">
    	<!--  -->
    	<class name="Article" table="dtb_article">
    		<id name="arti_id">
    			<generator class="native"></generator>
    		</id>
    		<property name="title" ></property>
    		<property name="content" ></property>
    		<property name="status" ></property>
    		
    		<many-to-one name="category" column="arti_cate_id" class="Category"></many-to-one>
    	</class>
    </hibernate-mapping>


使用el表达式和jstl标签获取登录用户的专栏和文章:
          <!-- 获取专栏以及专栏下的文章  -->
					<div id="categories" class="easyui-tree"  border=false >
						<c:forEach var="cate" items="${loginUser.categories }">
							<b><p style="font-size:150%">${cate.title }</p></b>
							<c:forEach var="arti" items="${cate.articles }">
								<p>${arti.title }</p>
							</c:forEach>
						</c:forEach><br>
					</div>

页面效果(红框内):

font_01

同样的方式展示数据,修改成专栏和文章后台管理页面:

<b><span style="display:inline-block;width:70%">专栏名</span> <span style="display:inline-block;width:10%">文章数</span> <span align="center" style="display:inline-block;width:10%">操作</span></b>
			   	 </br>
			   	 </br>
			   	 <c:forEach var="cate" items="${loginUser.categories }">
			   	 <p>
			   	 	<a style="display:inline-block;width:70%">${cate.title }</a>
			   	 	<a align="center" style="display:inline-block;width:10%">${cate.articles.size() }</a>
			   	 	<a style="display:inline-block;width:10%">编辑 | 删除</a>
			   	 	</br></br>
			   	 </p>
			   	 </c:forEach>
页面效果:

font_02

文章管理:

 <b><span style="display:inline-block;width:70%">文章</span> <span style="display:inline-block;width:10%">文章类别</span> <span align="center" style="display:inline-block;width:10%">操作</span></b>
			   	 </br>
			   	 </br>
			   	 <c:forEach var="cate" items="${loginUser.categories }">
			   	 	<c:forEach var="article" items="${cate.articles }">
			   		<p>
			   	 	<a style="display:inline-block;width:70%">${article.title }</a>
			   	 	<a align="center" style="display:inline-block;width:10%">${article.category.title }</a>
			   	 	<a style="display:inline-block;width:10%">编辑 | 删除</a>
			   	 	</br></br>
			   	 	</p>
			   	 	</c:forEach>
			   	 </c:forEach>
文章管理页面:

font_03


展示功能完成,接下来要给这些隐私页面加点保护,通过拦截器在进入action前拦截不合法的请求并将其重定向到登录页面。

先将专栏管理和文章管理页面放到/WEB-INF/pages/下,然后在struts配置文件中加入配置,通过action访问jsp页面:

		<!-- 配置访问权限页面方法-->
		<action name="*_page">
			<result type="dispatcher">/WEB-INF/pages/{1}.jsp</result>
		</action>


自定义拦截器:

/*
 * 权限拦截器
 * 校验用户是否已经登录
 */
public class LoginInterceptor extends MethodFilterInterceptor{

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		//获得session
		Map<String, Object> session = ActionContext.getContext().getSession();
		//判断登录标识是否为null
		Object user = session.get("loginUser");
		if(user != null){
			//已登录,继续执行拦截器链
			invocation.invoke();
		}
		//未登陆,拦截跳转到登陆页面
		return "toLogin";
	}

}

配置拦截器和全局结果集:

		<interceptors>
			<!-- 配置自定义拦截器 -->
			<interceptor name="loginInterceptor" class="com.dengtuzi.dtb.web.interceptor.LoginInterceptor"></interceptor>
			<!-- 配置拦截器栈 -->
			<interceptor-stack name="newStack">
				<interceptor-ref name="loginInterceptor">
					<param name="excludeMethods">login</param>
				</interceptor-ref>
				<interceptor-ref name="defaultStack"></interceptor-ref>
			</interceptor-stack>	
		</interceptors>
		<!-- 设置使用的拦截器栈 -->
		<default-interceptor-ref name="newStack"></default-interceptor-ref>
		<!-- 配置全局结果集 -->
		<global-results >
			<result name="toLogin" type="redirect">/login.jsp</result>
		</global-results>

测试结果:

login_interceptor


### 如何在优快云平台上发布专栏文章 要在 优快云 平台上发布专栏文章,需遵循一系列操作流程来确保文章能够顺利上线并被读者访问。以下是关于如何创建发布专栏文章的具体说明: #### 创建专栏的前提条件 通常情况下,在 优快云 上开设专栏可能需要满足一定的资格要求,比如成为认证博客专家或其他形式的身份验证[^2]。如果尚未获得相应权限,则可以通过提升个人影响力、撰写高质量的文章以及积极参与社区互动等方式逐步达到标准。 #### 编辑与发布过程 一旦具备了开设专栏的权利之后,实际的操作步骤与其他普通文章类似,主要区别在于设置为“专栏”属性这一环节。具体来说: 1. 登录至您的 优快云 账号; 2. 进入后台管理页面找到新增加或者编辑已有稿件的功能入口; 3. 按照常规方式完成内容输入框内的文字录入工作的同时注意调整版面布局使其更美观易读; 4. 当所有正文部分准备完毕以后,请务必前往顶部菜单栏寻找有关分类标签的选择项,并从中挑选出代表自己所要投稿方向的那个类别——这里即为我们所说的“专栏”选项[^3]; 5. 继续补充其他必要信息如摘要描述等字段后提交审核即可等待官方批准通过后再正式对外展示出来供大众阅览学习参考之用了! ```python # 示例代码:虽然此场景无需编程实现,但提供一段简单的Python函数作为示范 def publish_column_article(title, content): """ 假设这是一个用于模拟发布专栏文章的函数 参数: title (str): 文章标题 content (str): 文章主体内容 返回: str: 成功消息或错误提示 """ try: # 验证逻辑省略... article_id = save_to_database(title, content) # 将数据存储到数据库中 mark_as_published(article_id) # 设置状态为已发布 return f"Article '{title}' has been successfully published." except Exception as e: return f"Failed to publish the article due to {e}" ``` #### 后续维护事项 即使已经成功发布了某篇专栏作品,作者仍需持续关注其表现情况以便及时作出改进措施。这包括但不限于定期更新现有资料库中的陈旧知识点、积极回复评论区里的疑问讨论等内容管理工作[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值