1 建立一個web項目。css
2 導入必要的JAR文件。html
放在WEB-INF下lib包裏。java
3 添加web.xml配置,添加啓動配置。web
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>StrutsDemo</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <!-- struts2.1.3以後的版本,能夠在該過濾器以前之間定義必定的過濾器--> 13 <!-- 定義struts2 的核心控制器,用於生成ActionMapper ,攔截全部的Action請求--> 14 <filter> 15 <filter-name>struts2</filter-name> 16 <filter-class> 17 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter 18 </filter-class> 19 </filter> 20 <filter-mapping> 21 <filter-name>struts2</filter-name> 22 <url-pattern>/*</url-pattern> 23 </filter-mapping> 24 25 </web-app>
4 編寫Action。apache
咱們來寫個用戶登陸驗證,提供用戶名和密碼兩個屬性。若是正確返回success不然返回error。app
1 package com.cy.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class LoginAction extends ActionSupport { 6 7 private static final long serialVersionUID = 1L; 8 9 private String userName; 10 private String password; 11 12 public String execute() { 13 14 if (userName.equals("hellokitty") && password.equals("123")) { 15 16 return SUCCESS; 17 } else { 18 return ERROR; 19 } 20 21 } 22 23 public String getUserName() { 24 return userName; 25 } 26 27 public void setUserName(String userName) { 28 this.userName = userName; 29 } 30 31 public String getPassword() { 32 return password; 33 } 34 35 public void setPassword(String password) { 36 this.password = password; 37 } 38 39 }
Action有一下特色:框架
5 添加框架核心配置文件struts.xml文件:在WEB-INF/classes文件夾下建立struts.xml。jsp
在struts2-core-2.3.16.jar中有strust-defalut.xml.咱們須要繼承它。post
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <package name="default" extends="struts-default"> 8 <action name="login" class="com.cy.action.LoginAction"> 9 <result name="success">/jsp/success.jsp</result> 10 <result name="error">/jsp/error.jsp</result> 11 </action> 12 </package> 13 14 15 </struts>
6 編寫界面 單元測試
6.1 login.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'Login.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <form action="login" method="post"> 27 用戶名:<input type="text" name="userName"><br/> 28 密 碼:<input type="password" name="password"/><br/> 29 <input type="submit" value="提交"/> 30 <input type="reset" value="重置"/> 31 </form> 32 </body> 33 </html>
6.2 success.jsp
<body> <h1>登陸成功!</h1> </body>
6.3 error.jsp
<body> <h1>登陸失敗!</h1> </body>
總體: