Struts2是個優秀的MVC框架。有人說java的框架是把簡單的東西搞得複雜,而我不這樣認爲。學習框架以前可能會這樣認爲,學了以後就會感受框架固然是在簡化問題,不然也不會有這麼多人用了。本文介紹如何用eclipse如何建立一個Struts2基於註解的登錄驗證動態網頁工程。通俗的說來,咱們用瀏覽器發送一個請求到服務器,服務器驗證經過則進入歡迎頁,不然給出錯誤提示。html
在eclipse中新建名爲Struts2Demo項目,要在項目中使用Struts2框架,先要下載Struts2的相關jar包,在本站能夠很容易搜索到。而後把須要的jar包添加到項目的lib目錄中。項目各文件結構以下圖:java
爲了使項目支持Struts2框架,配置web.xml文件:web
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
再配置struts.xml文件,此文件必定要位於src文件目錄下,不然配置無效。我作此項目時因爲此文件沒放在src目錄下,而是放在了action所在的包中,致使一直運行不出來,浪費的半天時間。 struts.xml內容以下:apache
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <constant name="struts.i18n.reload" value="true" /> <constant name="struts.configuration.xml.reload" value="true" /> <constant name="struts.action.extension" value="do,action,," /> </struts>
再看用戶的請求頁面:瀏覽器
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Welcome User</title> <s:head /> </head> <body> <s:form action="welcome"> <s:textfield name="userName" label="User Name" /> <s:submit /> </s:form> <s:debug></s:debug> </body> </html>
上面的頁面中form的action爲welcome意思是頁面提交到名爲welcome頁面或action處理。產後的Url請求爲http://localhost:8080/Struts2Demo/welcome。而後咱們建立一個處理此請求的action。服務器
package org.sunhing.actions; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Result; import com.opensymphony.xwork2.ActionSupport; @Action(value="/welcome",results={ @Result(name = "success", location = "/welcome.jsp"), @Result(name = "input", location = "/index.jsp")} ) public class Welcome extends ActionSupport{ private static final long serialVersionUID = 1L; private String userName; private String message; public String execute() { setMessage("Hello " + getUserName()); return SUCCESS; } @Override public void validate() { if("".equals(userName)){ addFieldError("userName", "用戶名不能爲空!"); } } public void setUserName(String userName) { this.userName = userName; } public void setMessage(String message) { this.message = message; } public String getUserName() { return userName; } public String getMessage() { return message; } }
以上代碼中的註解:
@Action(value="/welcome",results={ @Result(name = "success", location = "/welcome.jsp"), @Result(name = "input", location = "/index.jsp")} )
意思即爲當咱們的請求爲/weclome(寫全了爲http://localhost:8080/Struts2Demo/welcome)時此請求交給此action處理。當上面的validate函數中校驗有錯誤時會返回"input",此時Struts2找到要跳轉的jsp頁面index.jsp,以下左圖。app
若校驗經過則跳轉到welcome.jsp,如上右圖。下面是welcome.jsp頁面代碼:框架
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Insert title here</title> </head> <body> <s:property value="message"></s:property> <s:debug></s:debug> </body> </html>
總結一下,Struts2爲咱們封裝了不少經常使用的功能,避免了咱們「重複造輪子」。
eclipse