1.從apache官網下載struts2(v2.2.1)的壓縮文件,解壓縮css
2.打開myeclipse(v8.6),將struts2解壓縮後lib下的ongl,strtus2-core,xwork-core,commons-logging,freemaker,javassist(該包須要另外下載,老版本里不須要)這六個必須包導入html
在struts2(v2.2.3.1)除了上面包外還需導入commons-io,commons-lang,commons-fileuploadjava
3.配置web.xml(WEB-INF下)web
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
老版本中用的過濾器是Dispatch類apache
4.編寫用戶登陸表單app
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="Login" method="post"> <Table> <Tr> <td>用戶名:</Td> <td><input type="text" name="username" /></Td> </Tr> <Tr> <td>密碼:</Td> <td><input type="password" name="password" /></Td> </Tr> <Tr> <td><input type="submit" name="submit" value="提交"/></Td> <td><input type="reset" name="reset" value="重置"/></Td> </Tr> </Table> </form> </body> </html>
5.編寫LoginAction.javaeclipse
package com.struts2.action; public class LoginAction { private String username; private String password; public String execute() throws Exception { if(this.getUsername().equals("tom")&&this.getPassword().equals("111111")){ return "success"; }else{ return "error" ; } } 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; } }
6.配置strtus.xml(src下)jsp
<?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> <package name="strutsqs" extends="struts-default"> <action name="Login" class="com.struts2.action.LoginAction"> <result name="success">/welcome.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
7.結果頁面就一句話,就不寫了post