1:login.JSP
<form action="
login.action" method="post">
username:<input type="text" name="username"> <br/>
password:<input type="text" name="password"> <br/>
<input type="submit" name="submit"/>
</form>
<s:form action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"> </s:password>
<s:submit name="submit"></s:submit>
</s:form>
2:action
package com.test.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() throws Exception {
return "success";
}
}
package com.test.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
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() throws Exception {
if("smallfa".equals(this.getUsername().trim())&&"smallfa".equals(this.getPassword().trim()))
{
return "success";
}
else
{
this.addFieldError(username, "username or password errors");
return "failer";
}
}
@Override
public void validate() {
if(null==this.getUsername()||"".equals(this.getUsername().trim()))
{
this.addFieldError(username, "username required");
}
if(null==this.getPassword()||"".equals(this.getPassword().trim()))
{
this.addFieldError(username, "username required");
}
}
}
3:struts-xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="struts2" namespace="/" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
4: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">
<filter>
<!-- 定义核心Filter的名字 -->
<filter-name>struts2</filter-name>
<!-- 定义核心Filter的实现类 -->
<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>
5:result.jsp
username:${requestScope.username}<br/>
password:${requestScope.password}<br/>
6:参考视频
视频:
Struts2入门与配置 浪曦 J2EE Struts 2应用开发详解系列视频
http://v.youku.com/v_show/id_XNTY4MDUwMzY=.html