1.首先在Myeclipse新建一个Web Project项目.并配置struts的环境.
2.struts2的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">
<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>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></web-app>
3.struts.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Sofeware Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Action所在包的定义 -->
<package name="C01" extends="struts-default">
<!-- 全局导航页面定义 -->
<global-results>
<result name="global">/jsp/login.jsp</result>
</global-results>
<!-- Action名字,类以及导航页面定义 -->
<!-- 通过Action类处理才导航的Action定义 -->
<action name="Login" class="action.LoginAction">
<result name="input">login.jsp</result>
<result name="success">success.jsp</result>
</action>
<!-- 直接导航的Action定义 -->
<action name="index">
<result>/jsp/login.jsp</result>
</action>
</package>
</struts>
4.action的内容如下:LoginAction
package action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private static String FORWARD = null;
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 void validate(){
//返回错误信息键值,内容在messageResource.
if(getUsername()==null||getUsername().trim().equals(""))
addFieldError("username",getText("user.required"));
if(getPassword()==null||getPassword().trim().equals(""))
addFieldError("password",getText("password.required"));
}
public String execute() {
username = getUsername();//属性值即JSP页面上输入的值
password = getPassword();//属性值即JSP页面上输入的值
try {
if (username != null && !username.equals("") && password != null
&& !password.equals("")) {
ActionContext.getContext().getSession()
.put("user", getUsername());
FORWARD = "success";//操作成功的页面
} else {
FORWARD = "input";//操作失败的页面
}
} catch (Exception e) {
e.printStackTrace();
}
return FORWARD;
}
}
5.jsp页面.
<!--login.jsp-->
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
</head>
<body>
<!-- form标签库定义,以及调用那个Action声明 -->
<s:form action="Login" method="post">
<table width="60%" height="76" border="0">
<!-- 各标签定义 -->
<s:textfield name="username" label="用户名"/>
<s:password name="password" label ="密码"/>
<s:submit value="登录" align="center"></s:submit>
</table>
</s:form>
</body>
</html>
<!--success.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>
</head>
<body>
<!-- 取得session中的用户名值 -->
${sessionScope.user },欢迎您~~
<br>
</body>
</html>
6.实现后的效果:
当用户名和密码都输入,成功后的显示: