首先在lib 中导入相关包Struts2相关jar包 可以在此下载 Struts2.1.6 jar包下载
配置 web.xml文件
将以下 filter 加入:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
===============================================================================================
UserDAO类:
package xuyan.com;
public class UserDAO {
public boolean Userlogin(User u)
{
if(u.getUsername().equals("admin")&&u.getPassword().equals("123456"))
{
return true;
}
else
{
return false;
}
}
}
================================================================================================
写一个LoginAction类;
package xuyan.com.action;
import xuyan.com.User;
import xuyan.com.UserDAO;
public class LoginAction {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute()
{
User u=new User();
u.setUsername(this.username);
u.setPassword(this.password);
UserDAO dao=new UserDAO();
if(dao.Userlogin(u))
{
return "Login_success";
}
else
{
return "Login_false";
}
}
}
===================================================================================
加入一个 struts.xml 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="Login" class="xuyan.com.action.LoginAction">
<result name="Login_success">/Success.jsp</result>
<result name="Login_false">/Falselogin.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
=========================================================================
Login.jsp页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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>
<form action="Login.action" method="post">
用户名:<input type="text" name="username"> <br>
密码: <input type="text" name="password"> <br>
<input type="submit" value="登陆"> <br>
</form>
</body>
</html>
================================================================================================
注意:1、
<form action="Login.action" method="post">
中必须为
action="Login.action" 与 <action
name="Login" class="xuyan.com.action.LoginAction"> 对应
class="xuyan.com.action.LoginAction" 为你写的 LoginAction类 的包地址
2、
public class LoginAction {
private String username; 与 Login.jsp 页面中: 用户名:<input type="text"name="username"> 参数一致
private String password; 与 Login.jsp 页面中: 用户名:<input type="text"name="password"> 参数一致
3、
public class LoginAction 类中 方法必须为: execute() -------------------------(Struts2默认方法)