使用MyEclipse(实现用户登录)
1.打开MyEclipse,需要注意的是这里应该处于MyEclipse Java Enterprise视图;新建一个Web Project,输入适当的工程名字,这里输入mystruts1,Finish即可。
2.添加struts1.2框架支持
在mystruts1工程上面右击,在弹出的菜单中选择MyEclipse -> Add Struts Capabilities…,添加Struts的支持。
3.新建三个Jsp页面
login.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <html:html lang="true"> <head> <title>login.jsp</title> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h2>用户登陆</h2> <html:form action="login.do" method="post"> 用户名:<html:text property="name"></html:text><br> 密码:<html:password property="password"></html:password><br> <html:submit value="登陆"></html:submit> <html:reset value="重置"></html:reset> </html:form> </body> </html:html>
login_success.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <html:html lang="true"> <head> <title>login.jsp</title> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1>登陆成功!!!</h1> </body> </html:html>
login_failure.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %> <html:html lang="true"> <head> <title>login.jsp</title> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h1>登录失败!!!</h1> </body> </html:html>
java类--ActionForm
package demo;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm {
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;
}
}
Action类
package demo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
String name=loginForm.getName();
String password=loginForm.getPassword();
if("admin".equals(name)&&"123".equals(password))
{
//跳转到成功页
return mapping.findForward("suc");
}
else
{
//跳转到失败页
return mapping.findForward("fal");
}
}
}
配置文件 web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <!-- ActionForm配置 --> <form-beans> <form-bean name="LoginForm" type="demo.LoginForm"></form-bean> </form-beans> <global-exceptions /> <!-- 全局转发配置 --> <global-forwards> <forward name="suc" path="/login_success.jsp"></forward> <forward name="fal" path="/login_failure.jsp"></forward> </global-forwards> <!-- Action 配置 --> <action-mappings> <action path="/login" type="demo.LoginAction" name="LoginForm" scope="request"> <forward name="suc" path="/login_success.jsp"></forward> <forward name="fal" path="/login_failure.jsp"></forward> </action> </action-mappings> <message-resources parameter="com.demo.struts.ApplicationResources" /> </struts-config>
部署项目,启动tomcat,这里就不详细说明!
最后打开浏览器,输入http://localhost:8080/mystruts1/,ok啦!