Create a new dynamic web project;
Enter the project name;
After New Runtime, select the following,
Select the installation directory of tomcat;
Then as follows, complete;
The current directory structure is as follows; the java environment must be configured before;
If you did not select Generate web.xml when creating the project, right-click the project folder and select the following menu to generate web.xml;
Copy the struts2 package to the lib folder, then refresh it and it will be displayed;
Select all packages, right-click, select the following menu, and add the package to the project;
Create struts.xml in the src folder,
struts.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>
Right click on the src folder and create a new package,
Enter the package name,
After Finish, as follows:
Right click on the newly created package and create a new class,
Enter the class name;
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";
}
}
}
Then create 3 more jsps;
index.jsp,
<%@ 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 and error.jsp are simplified, just add a string between <body></body>;
The project structure at this time is as follows:
There is also web.xml code;
<?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>
Then right-click the project folder and select the following menu,
The following dialog box appears, Finish;
index.jsp will not come out. Check the console output and it contains an error.
java.lang.ClassNotFoundException: .apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,
I want to put in web.xml
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
Change to
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
Then run it again, and the following error appears again; there is time to continue;