1.从apache官网下载struts2(v2.2.1)的压缩文件,解压缩
2.打开myeclipse(v8.6),将struts2解压缩后lib下的ongl,strtus2-core,xwork-core,commons-logging,freemaker,javassist(该包需要另外下载,老版本里不需要)这六个必须包导入
在struts2(v2.2.3.1)除了上面包外还需导入commons-io,commons-lang,commons-fileupload
3.配置web.xml(WEB-INF下)
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
03 | xmlns = "http://java.sun.com/xml/ns/javaee" |
04 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
05 | xsi:schemaLocation="http://java.sun.com/xml/ns/javaee |
06 | http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> |
08 | < filter-name >struts2</ filter-name > |
09 | < filter-class >org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</ filter-class > |
13 | < filter-name >struts2</ filter-name > |
14 | < url-pattern >/*</ url-pattern > |
19 | < welcome-file >index.jsp</ welcome-file > |
老版本中用的过滤器是Dispatch类
4.编写用户登录表单
01 | <%@ page language= "java" import = "java.util.*" pageEncoding= "utf-8" %> |
03 | String path = request.getContextPath(); |
04 | String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ; |
07 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > |
10 | <base href= "<%=basePath%>" > |
12 | <title>My JSP 'index.jsp' starting page</title> |
13 | <meta http-equiv= "pragma" content= "no-cache" > |
14 | <meta http-equiv= "cache-control" content= "no-cache" > |
15 | <meta http-equiv= "expires" content= "0" > |
16 | <meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" > |
17 | <meta http-equiv= "description" content= "This is my page" > |
19 | <link rel= "stylesheet" type= "text/css" href= "styles.css" > |
24 | <form action= "Login" method= "post" > |
29 | <td><input type= "text" name= "username" /></Td> |
33 | <td><input type= "password" name= "password" /></Td> |
36 | <td><input type= "submit" name= "submit" value= "提交" /></Td> |
37 | <td><input type= "reset" name= "reset" value= "重置" /></Td> |
5.编写LoginAction.java
01 | package com.struts2.action; |
03 | public class LoginAction { |
04 | private String username; |
05 | private String password; |
07 | public String execute() throws Exception |
09 | if ( this .getUsername().equals( "tom" )&& this .getPassword().equals( "111111" )){ |
16 | public String getUsername() { |
20 | public void setUsername(String username) { |
21 | this .username = username; |
24 | public String getPassword() { |
28 | public void setPassword(String password) { |
29 | this .password = password; |
6.配置strtus.xml(src下)
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <!DOCTYPE struts PUBLIC |
03 | "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
04 | "http://struts.apache.org/dtds/struts-2.0.dtd"> |
08 | < package name = "strutsqs" extends = "struts-default" > |
09 | < action name = "Login" class = "com.struts2.action.LoginAction" > |
10 | < result name = "success" >/welcome.jsp</ result > |
11 | < result name = "error" >/error.jsp</ result > |
7.结果页面就一句话,就不写了