使用Struts2開發程序的步驟html
1.導入jar包java
<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文件web
<!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.頁面apache
<%@ 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>
4.控制層api
public class UserAction implements Action{ //SpringMvc Controller public String execute() throws Exception { return SUCCESS; } }
5.Struts2.xmlapp
<?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.bdqn.action.UserAction"> <result name="success">/index.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
6.部署,運行項目jsp