Struts2回顾复习02
本次复习第一个项目,运用Struts完成个简单的登陆界面,温故知新。
首先建立项目,配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 8 <filter> 9 <filter-name>struts2</filter-name> 10 <filter-class> 11 org.apache.struts2.dispatcher.FilterDispatcher 12 </filter-class> 13 </filter> 14 15 <filter-mapping> 16 <filter-name>struts2</filter-name> 17 <url-pattern>/*</url-pattern> 18 </filter-mapping> 19 20 21 </web-app>
这是Struts2的配置,与1.0有区别。
login.jsp页面很简单,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% 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 'login.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.action" method="post"> username:<input type="text" name="username"><br> password:<input type="password" name="password"><br> <input type="submit" value="submit"> </form> </body> </html>
Struts.xml文件建立在src目录里面,然后myeclipse自动放在tomcat的classes文件夹中。如果忘记配置,那么可以在下载的帮助文档中,在app文件夹下找到一个项目,然后找到Struts.xml的头文件,然后复制就好了,不用记住。头文件配置为:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd">
然后建立一个包,com.test.action包,然后建立个LoginAction.java文件。
用过1.0开发过的人一定会理解,ActionForm是争议最大的。所以在2.0以上中取消了。只有一个Action,登录界面中的LoginAction.java文件代码为:
1 package com.test.action; 2 3 import java.util.Map; 4 5 import com.opensymphony.xwork2.ActionContext; 6 import com.opensymphony.xwork2.ActionSupport; 7 8 public class LoginAction extends ActionSupport 9 { 10 private String username; 11 private String password; 12 13 public String getUsername() 14 { 15 return username; 16 } 17 18 public void setUsername(String username) 19 { 20 this.username = username; 21 } 22 23 public String getPassword() 24 { 25 return password; 26 } 27 28 public void setPassword(String password) 29 { 30 this.password = password; 31 } 32 33 @SuppressWarnings("unchecked") 34 public String execute() throws Exception 35 { 36 if ("hello".equals(this.getUsername().trim()) 37 && "world".equals(this.getPassword().trim())) 38 { 39 Map map = ActionContext.getContext().getSession(); 40 41 map.put("user","valid"); 42 43 return "success"; 44 } 45 else 46 { 47 this.addFieldError("username", "username or password error"); 48 return "failer"; 49 } 50 51 } 52 53 @Override 54 public void validate() 55 { 56 if (null == this.getUsername() || "".equals(this.getUsername().trim())) 57 { 58 this.addFieldError("username", "username required"); 59 } 60 if (null == this.getPassword() || "".equals(this.getPassword().trim())) 61 { 62 this.addFieldError("password", "password required"); 63 } 64 } 65 66 }
看到这个程序,我们发现,他不依赖任何的类,没有Form等等,需要注意的是:如果客户端定义个username,那么Struts会自动去找Action中的getUserName,然后进行一系列操作,找的是getUserName,并不是private中的属性username,而是根据方法来执行。
然后新建个jsp页面,用来显示结果代码也很简单:
1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> 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 'result.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 27 username:<%= request.getParameter("username") %><br> 28 password:<%= request.getParameter("password") %> 29 30 </body> 31 </html>
现在我们配置Struts.xml文件:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <package name="struts2" extends="struts-default"> 9 10 <action name="login" class="com.test.action.LoginAction">
11 <result name="success">/result.jsp</result> 12 13 </action> 14 15 </package> 16 17 </struts>
熟悉Struts1.0的可能知道,这里面的class属性相当于1.0中的type属性。其中的name属性"login",对应login.jsp中的action="login.action",Struts遇到login.action会自动进行Action处理,并且将login直接传到Struts.xml中的login,自动调用com.test.action.LoginAction生成对象,这样就转移到了LoginAction.java文件,这里需要注意的是,执行LoginAction时,会自动执行execute方法,返回success(struts中的默认值)。然后到Struts.xml中的success,对应的/result.jsp页面显示。这样我们就配置好了Struts.xml
完成后运行就可以了。