STRUTS2做登陆的简单实现
第一步:创建一个WEB项目.
第二步:倒入STRUTS2的.jar包.
第三步:在src下创建一个struts.xml文件:
<struts>
-
<
action name
="
forwardLogin
"
method
="
forward
"
class
="
com.sinoest.login.action.LoginAction
">
<
result
>
/Login.jsp
</
result
>
</
action
>
<
result name
="
input
">
/Login.jsp
</
result
>
<
result
>
/Welcome.jsp
</
result
>
</
action
>
</
package
>
</
struts
>
第四步:配置web.xml文件:
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<jsp-config>
<taglib>
<taglib-uri>struts-tags</taglib-uri>
<taglib-location>
/WEB-INF/struts2-core-2.0.14.jar/struts-2.0.dtd
</taglib-location>
</taglib>
</jsp-config>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<jsp-config>
<taglib>
<taglib-uri>struts-tags</taglib-uri>
<taglib-location>
/WEB-INF/struts2-core-2.0.14.jar/struts-2.0.dtd
</taglib-location>
</taglib>
</jsp-config>
第五步:写ACTION文件:
package com.sinoest.login.action;
import javax.servlet.http.HttpSession;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.ServletActionContext;
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 7315440691690047462L;
*
*/
private static final long serialVersionUID = 7315440691690047462L;
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String forward() throws Exception {
return SUCCESS;
}
@Override
public String execute() throws Exception {
if("admin".equals(name)) {
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute("name", name);
return SUCCESS;
} else {
return INPUT;
}
}
}
第六步:创建/login.jsp和/welcome.jsp文件.