<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 导入自定义标签 -->
<%@taglib uri="http://com.bide.jstl.web.util/role" prefix="role"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<style type="text/css" >
.style1
{
color:#a67090;
font-family:10px;
font-weight:bold;
}
</style>
</head>
<body>
<SPAN class="style1"><role:role roleName="num1">测试1</role:role></SPAN> <br>
<SPAN class="style1"><role:role roleName="num2">测试2</role:role></SPAN> <br>
<SPAN class="style1"><role:role roleName="num3">测试3</role:role></SPAN> <br>
<SPAN class="style1"><role:role roleName="num4">测试4</role:role></SPAN> <br>
</body>
</html>
action中代码:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.bide.jstl.web.action;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.bide.jstl.model.prisist.entity.Login;
import com.bide.jstl.model.services.CheckLogin;
import com.bide.jstl.util.AllGetDispatcher;
import com.bide.jstl.web.form.UserloginForm;
public class UserloginAction extends Action implements AllGetDispatcher{
private CheckLogin checkLogin;
public CheckLogin getCheckLogin() {
return checkLogin;
}
public void setCheckLogin(CheckLogin checkLogin) {
this.checkLogin = checkLogin;
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserloginForm userloginForm = (UserloginForm) form;
//Action只做业务控制,不实现业务逻辑
Login login =new Login();
login.setName(userloginForm.getStrName());
login.setPassword(userloginForm.getStrPwd());
HttpSession session=request.getSession();
Set setRole=checkLogin.CheckUserlogin(login);
if(null!=setRole)
{
//存放至作用域,然后跳转,再标签处理类里面做判断
session.setAttribute("setRole", setRole);
return mapping.findForward(SUCCESS);
}
else
return mapping.findForward(ERROR);
// TODO Auto-generated method stub
}
}
自定义标签类:
package com.bide.jstl.util;
import java.util.Set;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class TagClass extends BodyTagSupport{
//封装属性
private String roleName;
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
//自定义标签处理类继承至BodyTagSupport并重写doStrartTag或者doEndTag方法,该方法返回的是一个整形数值.
public int doStartTag()
{
Set setRole=(Set) pageContext.getSession().getAttribute("setRole");
if(null!=setRole&&setRole.contains(roleName))
{
return EVAL_BODY_INCLUDE;
}
return this.SKIP_BODY;
}
}