action:(注意登陆方法中的代码)
package cn.net.xsoft.action;
import java.util.List;
import cn.net.xsoft.bo.ArticleBO;
import cn.net.xsoft.bo.ArticleTypeBO;
import cn.net.xsoft.bo.LoginBO;
import cn.net.xsoft.po.Article;
import cn.net.xsoft.po.ArticleType;
import cn.net.xsoft.po.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* action
* @author H
*
*/
public class LoginAction extends ActionSupport{
private ArticleBO articleBO;
private ArticleTypeBO articleTypeBO;
private LoginBO loginBO;
private String password;
private String username;
public ArticleBO getArticleBO() {
return articleBO;
}
public void setArticleBO(ArticleBO articleBO) {
this.articleBO = articleBO;
}
public ArticleTypeBO getArticleTypeBO() {
return articleTypeBO;
}
public LoginBO getLoginBO() {
return loginBO;
}
public String getPassWord() {
return password;
}
public String getUserName() {
return username;
}
//登陆
public String login() throws Exception {
User user = loginBO.checkLogin(username, password);
System.out.println(user);
if(user!=null){
/**
* 为了显示登陆之后的博文类别
*
*/
List<ArticleType> type_list = articleTypeBO.searchArticleType(); //type_list已经存储了ArticleType对象
System.out.println(type_list);
System.out.println(type_list.get(0).getTypeName());
ActionContext.getContext().getSession().put("type_list",type_list); //存储在session中
/**
* 为了显示登陆之后的全部博文
*/
List<Article> article_list = articleBO.searchArticle(); //article_list中已经储存了Article对象
System.out.println(article_list);
ActionContext.getContext().getSession().put("article_list", article_list); //存储在session中
return this.SUCCESS;
}
return this.ERROR;
}
//注销
public String logout() throws Exception {
ActionContext.getContext().getSession().remove("usr");
ActionContext.getContext().getSession().clear();
return this.SUCCESS;
}
public void setArticleTypeBO(ArticleTypeBO articleTypeBO) {
this.articleTypeBO = articleTypeBO;
}
public void setLoginBO(LoginBO loginBO) {
this.loginBO = loginBO;
}
public void setPassWord(String passWord) {
this.password = passWord;
}
public void setUserName(String userName) {
this.username = userName;
}
}
jsp:(显示)
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--网页头部分-->
<!--<s:include value="head_top.jsp"></s:include>-->
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<table width="291" border="0" align="center" cellpadding="0" cellspacing="0">
<s:iterator value="#session.type_list" status="id" id="id" >
<td>
<s:a href="#" class="button icon-gear" title="查看详情"><s:property value="typeName" /></s:a>
</td>
</s:iterator>
</table>
<table width="380" border="0" align="center">
<s:iterator value="#session.article_list" status="id" id="id" >
<tr>
<tr>
<td>
<s:a href="#" class="button icon-gear" title="查看详情"><s:property value="title" /></s:a>
</td>
</tr>
<tr>
<td>
<s:a href="#" class="button icon-gear" title="查看详情"><s:property value="content" /></s:a>
</td>
</tr>
</tr>
</s:iterator>
</table>
</table>
<!--网页尾部分-->
<s:include value="head_down.jsp"></s:include>
</body>
</html>
效果图: