apptld
<?xml version= encoding=UTF?>
<taglib>
<tlibversion></tlibversion>
<jspversion></jspversion>
<tag>
<name>display</name>
<tagclass>comtestDisplayTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>page</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
DisplayTagjava
package comtest;
import javautilList;
import javaxservletJspException;
import javaxservletjspJspTagException;
import javaxservletjspJspWriter;
import javaxservletjsptagextTagSupport;
import comtestHibernateUser;
public class DisplayTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = L;
private String name = alluser;
private String page;
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public String getPage() {
return page;
}
public void setPage(String page) {
thispage = page;
}
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
thistest = test;
}
@Override
public int doStartTag() throws JspException {
return SKIP_BODY;
}
public int doEndTag() throws JspException {
boolean valid = false;
HttpSession session = pageContextgetSession();
if (session != null && sessiongetAttribute(name) != null) {
valid = true;
} else {
try {
pageContextforward(page);
} catch (Exception e) {
// TODO: handle exception
throw new JspException(etoString());
}
}
JspWriter out = pageContextgetOut();
try {
List users = (List) sessiongetAttribute(alluser);
if (valid) {
outprintln(<table border=\\ width=% height=%>);
outprintln(<tr>);
outprintln(<th>ID</th>);
outprintln(<th>Name</th>);
outprintln(<th>password</th>);
outprintln(</tr>);
for (int i = ; i < userssize(); i++) {
User user = (User) usersget(i);
outprintln(<tr>);
outprintln(<td> + usergetId() + </td>);
outprintln(<td> + usergetUsername() + </td>);
outprintln(<td> + usergetPassword() + </td>);
}
outprintln(</table>);
} else {
outprintln(null);
}
} catch (Exception e) {
// TODO Autogenerated catch block
throw new JspTagException(IOException: + etoString());
}
return SKIP_PAGE;
}
@Override
public void release() {
// TODO Autogenerated method stub
superrelease();
thisname = alluser;
thispage = loginjsp;
}
}
LoginActionjava
package comteststrutsaction;
import javautilList;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import orgapachestrutsactionAction;
import orgapachestrutsactionActionForm;
import orgapachestrutsactionActionForward;
import orgapachestrutsactionActionMapping;
import orgapachestrutsvalidatorDynaValidatorForm;
import comtestHibernateUserDAO;
public class LoginAction extends Action {
private UserDAO userDao;
public UserDAO getUserDao() {
return userDao;
}
public void setUserDao(UserDAO userDao) {
thisuserDao = userDao;
}
public ActionForward execute(ActionMapping mapping ActionForm form HttpServletRequest request HttpServletResponse response) throws Exception {
DynaValidatorForm loginForm = (DynaValidatorForm) form;
String username = loginFormgetString(username);
String password = loginFormgetString(password);
if (userDaoisValidUser(username password)) {
List users = userDaoshowAll();//在userDao中写操作数据库的方法其中showAll()方法是取出数据库中说有数据
requestsetAttribute(user username);
requestgetSession()setAttribute(alluser users);
return mappingfindForward(indexGo);
} else {
return mappinggetInputForward();
}
}
}
使用
<%@ taglib uri=/WEBINF/apptld prefix=app %>
<app:display page=loginjsp/>