1.必备的struts2-blank.war下的lib目录下的所有驱动包,放入项目下的lib目录中。
2.新建一个UserAction类,代码如下:
package com.eduask;
import com.opensymphony.xwork2.ActionSupport;
//创建一个UserAction继承ActionSupport类;
public class UserAction extends ActionSupport{
private String userName;
private String userPassWord;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassWord() {
return userPassWord;
}
public void setUserPassWord(String userPassWord) {
this.userPassWord = userPassWord;
}
@Override
public String execute() throws Exception {
if("tom".equals(userName)&&"123456".equals(userPassWord))
return "success";
else
return "error";
}
}
3.在src目录下新建一个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>
<!--定义包管理配置的action继承struts-default.xml中的配置 -->
<!-- form标签中action的地址 -->
<package name="actions" extends="struts-default">
<action name="login" class="com.eduask.UserAction">
<!--成功页面-->
<result name="success">/success.jsp</result>
<!--失败页面-->
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
4.在webRoot目录下新建三个jsp文件,分别是success.jsp、error.jsp、index.jsp
5.success.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path=request.getContextPath();
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>my success jsp page</title>
</head>
<body>
<h1>
欢迎<%=request.getParameter("userName") %>登录<br>
</h1>
</body>
</html>
6.error.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path=request.getContextPath();
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>my error jsp page</title>
</head>
<body>
<h1>
用户名或密码错误
</h1>
</body>
</html>
7.index.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>
</head>
<body>
<form action="login.action" method="post">
用户名 <input name="userName" type="text"><br>
密码 <input name="userPassWord" type="password"><br>
<input type=submit name=subm value="提交">
<input type=reset name=reset value="取消">
</form>
</body>
</html>
8.最后修改web.xml的配置文件,修改结果为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--过滤器-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!--org.apache.struts2.dispatcher.FilteDispatcher -->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
9.部署项目到tomcat下,在浏览器中显示如下:
1.用户名和密码输入正确
显示: 该用户成功登录
2.用户名或密码输入不正确
显示: 用户名和密码错误