登陆页面
登陆成功 用户名:dll 密码:123
登陆失败:
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>
<!--Struts2的Action必须放在指定的空间下定义-->
<package name="default" namespace="/" extends="struts-default">
<!--定义login的Action,该Action的实现类为:orgdll.action.LoginAction类-->
<action name="login" class="com.dll.struts2.action.LoginAction">
<result name="error">/codes/02/error.jsp</result>
<result name="success">/codes/02/success.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2demo</display-name>
<!-- 定义Struts2的核心控制器:StrutsPrepareAndExcuteAndExcuteFilter -->
<filter>
<!-- 定义核心Filter的名字 -->
<filter-name>struts2</filter-name>
<!-- 定义核心Filter的实现类 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- StrutsPrepareAndExecuteFilter用来处理所有的HTTP请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
LoginAction.java
package com.dll.struts2.action;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action{
//下面是用于封装用户请求参数的两个属性
private String username;
private String password;
//username和password属性的getter和setter方法
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() throws Exception{
//当用户请求参数的username等于dll,密码请求参数为123时
//返回success字符串,否则返回error字符串
if("dll".equals(username) && "123".equals(password)){
return "success";
}else{
return "error";
}
}
}
login.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>登陆页面</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" methos="post">
<table>
<caption><h3>用户登录</h3></caption>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登陆"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>