在项目中添加struts:
1:点击项目右击选择myeclipse
2:选择Add Struts Capadilities
3:选择2.1
1.在src中兴建一个包,在包中新建一个class并继承ActionSupport接口,在里面重写execute()方法。
代码是:
package Classwork;
import com.opensymphony.xwork2.ActionSupport;
//@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
private String userID;
private String passWork;
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getPassWork() {
return passWork;
}
public void setPassWork(String passWork) {
this.passWork = passWork;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
if(userID.equals("aaa")&&passWork.equals("123"))
return "success";
else return "error";
}
}
2.在struts.xml中新建一个action(注意:在class中的execute()内返回的字符串“success”、“error”要与action中的name的值一样)
<package name="default" extends="struts-default">
<!-- 用户登录 -->
<action name="userlogin" class="Classwork.LoginAction">
<result name="success">/main.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
3.1在jsp中编辑登录界面
<body>
<form action="userlogin.action" method="post">
用户名:<input type="text" name="userID"><br>
密码 :<input type="text" name="passWork"><br>
<input type="submit" value="登录">
</form>
</body>
3.2在jsp中编辑登陆成功和失败页面(注意:保存格式改为UTF-8,登陆成功和失败页面的名字要与action中的“main.jsp”和“error.jsp”一样
)
登陆成功页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘main.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
登录成功! 欢迎<s:property value="userID"/>进入主界面
</body>
</html>
登陆失败页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘error.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
登录失败
</body>
</html>
4.对项目进行部署,在浏览器中输入:http://localhost:8080/myclass/userlogin.jsp
其结果是:
输入正确的用户名和密码:
其结果是:
输入错误密码:
其结果是: