===========================================
login.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<title></title>
</head>
<body>
<form action="LoginAction.action" method="post">><!--默认是get-->
name<input type="text" name=username /><!--username后面有空格-->
password<input type="password" name=password />
<input type="submit"/>
<input type="reset"/>
</form>
</body>
</html>
===========================================
web.xml(路径在web-inf下)
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
===========================================
struts.xml(路径在web-inf\classes下)
<?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="struts" extends="struts-default">
<action name="LoginAction" class="com.zyl.action.LoginAction">
<result name="success" type="dispatcher">/success.jsp</result>
<result name="login" type="redirect">/login.html</result>
</action>
</package>
</struts>
<!-- 1.跳转方式
dispatcher和chain是服务器端跳转,所以客户端只发起一次请求,产生一个action;
redirect和redirectAction是客户端跳转,所以客户端发起两次请求。
2.跳转内容
dispatcher和redirect跳转的是views(一般是jsp页面);
chain和redirectAction跳转的是一个action。-->
===========================================
LoginAction.java
package com.zyl.action;
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(){
if("zyl".equals(username)&&"123".equals(password)){
return "success";
}else{
return "login";
}
}
}
===========================================
success.jsp
<%@page contentType="text/html;charset=gb2312" pageEncoding="gb2312"%>
<%@ taglib uri="/struts-tags" prefix="s"%><!-=这里是s不是p-->
<s:property value="username"/><!--这里是value 不是name-->
<%out.println("成功");%>