創(chuàng)建項(xiàng)目
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的依賴(lài)包: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發(fā)布容器的使用
- pom.xml project節(jié)點(diǎn)下加入以下內(nèi)容
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.7</version>
</plugin>
</plugins>
</build>
- 將左側(cè)項(xiàng)目欄中的web文件夾重命名為webapp恃轩,并將其移動(dòng)至src/main文件下
- 不知道為什么jetty無(wú)法找到structs.xml文件的位置舆床,所以我們需要在web.xml中手動(dòng)初始化一下配置參數(shù)
<?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實(shí)例url:localhost:8080/{projectname}/{action}
org.mortbay.jetty:maven-jetty-plugin:6.1.7:run
-Djetty.port=8081
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者