创建项目
Add GroupId And ArtifactId
Add Project Name And Location
You Will See Some file Created
<?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>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<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>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
$END$
</body>
</html>
添加maven的依赖包:pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>helloweb</groupId>
<artifactId>helloweb</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
</project>
Jetty发布容器的使用
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.7</version>
</plugin>
</plugins>
</build>
- 将左侧项目栏中的web文件夹重命名为webapp,并将其移动至src/main文件下
- 不知道为什么jetty无法找到structs.xml文件的位置,所以我们需要在web.xml中手动初始化一下配置参数
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,src/main/java/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
其他源码
import java.io.Serializable;
public class User implements Serializable {
private String mUsername;
private String mPassword;
public String getUsername() {
return mUsername;
}
public void setUsername(String username) {
mUsername = username;
}
public String getPassword() {
return mPassword;
}
public void setPassword(String password) {
mPassword = password;
}
}
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.rtfsc.model.User;
public class UserAction extends ActionSupport implements ModelDriven<User> {
private User mUser = new User();
public User getModel() {
return mUser;
}
@Override
public String execute() throws Exception {
String username = mUser.getUsername();
System.out.println("username = " + username);
String password = mUser.getPassword();
System.out.println("password = " + password);
if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
return ERROR;
}
if (username.equals(password)) {
return SUCCESS;
} else {
return ERROR;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="home" namespace="/" extends="struts-default">
<default-action-ref name="index"/>
<action name="index">
<result>/index.jsp</result>
</action>
<action name="user" class="com.rtfsc.action.UserAction">
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
- jetty实例url:localhost:8080/{projectname}/{action}
org.mortbay.jetty:maven-jetty-plugin:6.1.7:run
-Djetty.port=8081