THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
I wrote this simple application to demostrate how to use webwork in a login/logout.
/Login.jsp
Code Block |
---|
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head><body> <form action="login.action" method="post"> User id<input type="text" name="userId" /> <br/> Password <input type="password" name="passwd" /> <br /> <input type="submit" value="Login"/> </form> </body> </html> |
/pages/welcome.jsp
Code Block |
---|
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" Â Â Â pageEncoding="ISO-8859-1"%> <%@ taglib prefix="ww" uri="/webwork" %> <jsp:include page="WEB-INF/inc/loginCheck.jsp" /> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Welcome</title> </head> <body>Welcome, you have logined. <br /> The attribute of 'context' in session is <ww:property value="#session.context" /> <br /><br /><br /> <a xhref="<%= request.getContextPath() %>/logout.action">Logout</a> <br /> <a xhref="<%= request.getContextPath() %>/logout2.action">Logout2</a> </body> </html> |
/WEB-INF/inc/loginCheck.jsp
Panel |
---|
<%@ taglib="/webwork" prefix="ww" %> |
simple.LoginAction.java
Code Block |
---|
 packagepackage simple; import java.util.Date;import java.util.Map; import javax.servlet.http.HttpSession; import com.opensymphony.webwork.ServletActionContext; import com.opensymphony.xwork.ActionSupport; public class LoginAction extends ActionSupport {    privateprivate String userId;    privateprivate String passwd;    publicpublic String execute() throws Exception {       if if ("admin".equals(userId) && "password".equals(passwd)) {          HttpSession// HttpSession session = ServletActionContext.getRequest().getSession();          session// session.setAttribute("logined","true");          session// session.setAttribute("context", new Date());          return SUCCESS;       }       return ERROR;    }       public// Better is using ActionContext Map session = ActionContext.getContext().getSession(); session.put("logined","true"); session.put("context", new Date()); return SUCCESS; } return ERROR; } public String logout() throws Exception {       HttpSession// HttpSession session = ServletActionContext.getRequest().getSession();       session// session.removeAttribute("logined");       session.removeAttribute// session.removeAttribute("context"); Map session = ActionContext.getContext().getSession(); session.remove("logined"); session.remove("context");       return return SUCCESS;    }    publicpublic String getPasswd() {       return return passwd;    }       public public void setPasswd(String passwd) {       this this.passwd = passwd;    }       public public String getUserId() {       return return userId;    }       public public void setUserId(String userId) {       this this.userId = userId;    } } |
simple.LogoutAction.java
Code Block |
---|
package simple; import java.util.Map; import javax.servlet.http.HttpSession; import com.opensymphony.webwork.ServletActionContext; import com.opensymphony.xwork.ActionSupport; public class LogoutAction extends ActionSupport { Â Â Â public String execute() throws Exception { Â Â Â Â Â Â HttpSessionMap session = ServletActionContextActionContext.getRequestgetContext().getSession(); Â Â Â Â Â Â session.removeAttributeremove("logined"); Â Â Â Â Â Â session.removeAttributeremove("context"); Â Â Â Â Â Â return SUCCESS; Â Â Â } } |
/WEB-INF/classes/xwork.xml
Code Block |
---|
 <<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.1.1//EN" "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd"> <xwork>    <include file="webwork-default.xml"/>    <package name="default" extends="webwork-default">        <!-- Add your actions here -->        <action name="login" class="simple.LoginAction" >           <result <result name="success" type="dispatcher">/pages/welcome.jsp</result>           <result <result name="error" type="redirect">/login.jsp</result>        </action>               <action name="logout2" class="simple.LoginAction" method="logout" >           <result <result name="success" type="redirect">/login.jsp</result>        </action>               <action name="logout" class="simple.LogoutAction" >           <result <result name="success" type="redirect">/login.jsp</result>        </action>    </package> </xwork> |