初学struts2-入门案列
1.所需类库
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.4.1
</version>
</dependency>
2.配置web-xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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.配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="userAction" class="cn.happy.action.UserAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
4.所需jsp页面
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<s:form method="POST" action="userAction">
<%--性能低--%>
请输入用户名: <s:textfield name="username"></s:textfield> <br/>
<s:textfield name="password"></s:textfield><br/>
<s:submit value="登陆"></s:submit>
</s:form>
</body>
</html>
5.配置action
package cn.happy.action;
import com.opensymphony.xwork2.Action;
/**
* Created by Happy on 2017-10-22.
* 类 Action
*/
public class UserAction implements Action{
//SpringMvc Controller
public String execute() throws Exception {
return SUCCESS;
}
}
6.项目部署,运行即可