struts2教程1

来源:互联网 发布:程序员 交易员 编辑:程序博客网 时间:2024/04/27 18:23

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<?xmlversion="1.0"encoding="UTF-8"?>
02<web-appversion="2.5" 
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">
07  <filter>
08    <filter-name>struts2</filter-name>
09    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
10  </filter>
11    
12  <filter-mapping>
13    <filter-name>struts2</filter-name>
14    <url-pattern>/*</url-pattern>
15  </filter-mapping>
16    
17    
18  <welcome-file-list>
19    <welcome-file>index.jsp</welcome-file>
20  </welcome-file-list>
21</web-app>

老版本中用的过滤器是Dispatch类

4.编写用户登录表单

 

01<%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>
02<%
03String path = request.getContextPath();
04String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
05%>
06  
07<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
08<html>
09  <head>
10    <base href="<%=basePath%>">
11      
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">
18    <!--
19    <link rel="stylesheet"type="text/css" href="styles.css">
20    -->
21  </head>
22      
23  <body>
24   <form action="Login"method="post">
25      
26    <Table>
27        <Tr>
28            <td>用户名:</Td>
29            <td><input type="text"name="username" /></Td>
30        </Tr>
31        <Tr>
32            <td>密码:</Td>
33            <td><input type="password"name="password" /></Td>
34        </Tr>
35        <Tr>
36            <td><input type="submit"name="submit" value="提交"/></Td>
37            <td><input type="reset"name="reset" value="重置"/></Td>
38        </Tr>
39    </Table> 
40      
41      
42    </form>
43  </body>
44</html>

5.编写LoginAction.java

01package com.struts2.action;
02  
03public class LoginAction {
04    privateString username;
05    privateString password;
06      
07    publicString execute() throwsException
08    {
09        if(this.getUsername().equals("tom")&&this.getPassword().equals("111111")){
10            return"success";
11        }else{
12            return"error" ;
13        }
14    }
15  
16    publicString getUsername() {
17        returnusername;
18    }
19  
20    publicvoid setUsername(String username) {
21        this.username = username;
22    }
23  
24    publicString getPassword() {
25        returnpassword;
26    }
27  
28    publicvoid setPassword(String password) {
29        this.password = password;
30    }
31      
32      
33      
34}

6.配置strtus.xml(src下)

01<?xmlversion="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">
05  
06<struts>
07     
08    <packagename="strutsqs" extends="struts-default">
09        <actionname="Login"class="com.struts2.action.LoginAction">
10            <resultname="success">/welcome.jsp</result>
11            <resultname="error">/error.jsp</result>
12        </action>
13    </package>
14</struts>

7.结果页面就一句话,就不写了