Strus2基礎:
Sturs2起源以及背景:
在起源很早(2002年左右)的 strus1 和 webWork 基礎上進行擴展,并且兼容這兩大框架!總之很好用啦,隨著學習的深入,應該會有更好的詮釋的!
新建strus2項目:
基礎架包:
strus2-core-***.jar 核心類庫
xwork-core-***.jar xwork類庫。構建基礎
ognl-**.jar 表達式語言類庫
freemarker-**.jar 標簽模板類庫
javassist-**.GA.jar 字節碼處理
commons-fileupload-**.jar 文件上傳
commons-io-**.jar io-擴展
Commons-lang-**.jar 數據類型的工具類
創建第一個strus2網頁:
以一個簡單的登錄操作來了解strus2執行過程
默認配置名:struts.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="test" class="demo.firstAction.FirstAction"> <result name="success">index.jsp</result> </action> <action name="login" class="demo.firstAction.FirstAction"> <result name="success">index.jsp</result> <result name="erro">erro.jsp</result> </action> </package> </struts>
action實現類:

package demo.firstAction; import com.opensymphony.xwork2.Action; public class FirstAction implements Action{ //用戶輸入姓名 private String name=""; //向用戶顯示的信息 private String message=""; @Override public String execute() throws Exception { if("李四".equals(name)){ this.setMessage("hello,"+this.getName()+"!"); return "success"; }else{ this.setMessage("用戶名:"+this.getName()+"登錄失敗!"); return "erro"; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <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>
頁面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!-- 導入strus2標簽庫 --> <%@ taglib uri="/struts-tags" prefix="s" %> <% 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 'index.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> <s:property value="message"/> <form action="login.action" method="post"> <input type="text" name="name"/> <input type="submit" value="登錄"> </form> </body> </html> <!--登錄失敗--> <body> <h1>登錄失敗界面</h1> <s:property value="message"/> </body>
執行流程:
提醒:
excute方法是Action接口的重寫方法 ,struts會默認執行這個方法
了解了他的執行過程,當出現exception時就好解決多 了!
下面是我今天遇到的兩個問題:
Strus2初步學習遇到的exception
錯誤一:
分析:
using Struts tags without the associated filter
沒有關聯過濾器
我只在頁面上寫了struts標簽卻沒去配置struts.xml&書寫相應的java代碼
解決辦法:
把代碼寫完整
錯誤二:啟動tomcat時報錯,提示class沒找到,這個類不是java類庫里面的從org/apeche/commons/lang3可以看出,所以導入缺少的架包
添加缺少的架包:問題解決
文章列表