struts2教程1

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"?>
02 <web-app version="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 <%
03 String path = request.getContextPath();
04 String 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

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

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">
05   
06 <struts>
07      
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>
12         </action>
13     </package>
14 </struts>

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

你可能感兴趣的:(html,MyEclipse,String,struts,filter,encoding)