新建动态web项目;

输入项目名称;

New Runtime后选择如下,

选择tomcat的安装目录;

然后如下,完成;

当前的目录结构如下;之前要配置好java环境;

如果创建项目时没有选中生成web.xml,右击项目文件夹,选择如下菜单,生成web.xml;

把struts2的包拷贝到lib文件夹下,然后刷新一下,会显示出来;

选中所有的包,右击,选择如下菜单,把包加入项目;

在src文件夹下创建struts.xml,

struts.xml,
XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- START SNIPPET: xworkSample -->
<struts>
<!-- 是否开启动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="login" class="com.example.struts2.LoginAction" method="login">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
右击src文件夹,创建新的包,

输入包名,

Finish之后如下,

右击新创建的包,创建新的类,

输入类名;

java
package com.example.struts2;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class LoginAction {
HttpServletRequest req = ServletActionContext.getRequest();
String username = req.getParameter("username");
String password = req.getParameter("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 login(){
if("xiaoBaby".equals(username)
&& "123456".equals(password)){
return "result";
}else{
return "error";
}
}
}
然后再创建3个jsp;
index.jsp,
html
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="post">
用户名:<input type="text" name="username">
密码:<input type="text" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
success.jsp和error.jsp简单化,在<body></body>之间添加一个字符串即可;
此时项目结构如下,

还有web.xml代码;
XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>sts</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<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>
然后右击项目文件夹,选择如下菜单,

出现如下对话框,Finish;

index.jsp不会出来,查看控制台输出,包含一个错误,
java.lang.ClassNotFoundException: .apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,
我想把web.xml中的
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
改为
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
然后再重新运行,又出现下图错误;有时间继续;
