国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
struts 2 配置方法及其技巧實(shí)例整理(一)

struts 2環(huán)境的配置:

把struts.xml文件拷貝到項(xiàng)目的src目錄下:

              STRUTS_HOME/apps/struts2-blank-2.1.6/WEB-INF/src/java/struts.xml  

       把相關(guān)jar文件拷貝到項(xiàng)目中:

              STRUTS_HOME /apps/struts2-blank-2.1.6/WEB-INF/lib 目錄下的相關(guān)jar文件

       配置web.xml文件:

              可以從

              STRUTS_HOME /apps/struts2-blank-2.1.6/WEB-INF/web.xml文件中拷貝:

              <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>

 

    實(shí)現(xiàn)最簡(jiǎn)單的的struts文件配置:

<!—配置struts常量,設(shè)置為開(kāi)發(fā)模式-->

     <constant name="struts.devMode" value="true" />

     <package name="default" namespace="/" extends="struts-default">

       

        <action name="hell">

            <result>

                /Hello.jsp

            </result>

        </action>

</package>

    設(shè)置struts標(biāo)簽的自動(dòng)提示功能:

       在MyEclipse Enterprise Workbench-->Files and Editors-->XML-->XML Catalog-->Add...添加文檔,選擇

\STRUTS_HOME\struts-2.1.6\lib\struts2-core-2.1.6\struts-2.0.dtd 文件,Key Type選擇URI,Key上填寫(xiě):

http://struts.apache.org/dtds/struts-2.0.dtd"

 

 

Namespace 命名空間:

    <package name="front" extends="struts-default" namespace="/front">

    如果namespace不寫(xiě),則相當(dāng)于:namespace=””;

 

 

Action:

    三種創(chuàng)建方法:

public class IndexAction1 {

    public String execute() {

       return "success";

    }

}

//只要有execute方法,返回了字符串,就可以被strut2認(rèn)為是action

 

public class IndexAction2 implements Action {

    @Override

    public String execute() {

       return "success";

    }

}

 

public class IndexAction3 extends ActionSupport {

    @Override

    public String execute() {

       return "success";

    }

}

//一般使用第三種方法,

struts配置文件:

        <action name="index" class="com.bjsxt.struts2.front.action.IndexAction1">

            <result name="success">/ActionIntroduction.jsp</result>

        </action>

 

 

Path路徑問(wèn)題:

JSP中的“/”代表的是服務(wù)器的跟路徑,而不是網(wǎng)站的根路徑,struts中不允許出現(xiàn)“//”兩個(gè)斜杠,一般使用絕對(duì)路徑:

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

<base href="<%=basePath%>" />

 

 

ActionMethod_DMI動(dòng)態(tài)方法:

        <action name="userAdd" class="com.bjsxt.struts2.user.action.UserAction" method="add">

    可以使用method屬性顯式調(diào)用add方法;

    也可以在url地址中動(dòng)態(tài)指定執(zhí)行的方法(action名!方法名):

    .../user!add

    推薦使用動(dòng)態(tài)指定的方式  

 

 

ActionMethod_通配符配置:

使用通配符,將配置量降到最低,不過(guò)一定要遵守“約定優(yōu)于配置”的原則。

        <action name="Student*" class="com.bjsxt.struts2.action.StudentAction" method="{1}">

            <result>/Student{1}_success.jsp</result>

        </action>

訪問(wèn)方法:       

<a href="<%=context %>/actions/Studentadd">添加學(xué)生</a>

 

 

        <action name="*_*" class="com.bjsxt.struts2.action.{1}Action" method="{2}">

            <result>/{1}_{2}_success.jsp</result>

            <!-- {0}_success.jsp -->

        </action>

訪問(wèn)方法:

<a href="<%=context %>/actions/Teacher_add">添加老師</a>

注意:如果在同一個(gè)package中,有一個(gè)請(qǐng)求符合兩個(gè)action的配置,會(huì)默認(rèn)匹配第一個(gè)。

 

 

用Action的屬性接收參數(shù):

URL訪問(wèn):.../user/user!add?name=a&age=1

要在action中獲取參數(shù),需要添加兩個(gè)屬性,并加上getter和setter方法:

    private String name;

    private int age;

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public int getAge() {

       return age;

    }

    public void setAge(int age) {

       this.age = age;

    }

 

DomainModel接收參數(shù):

URL訪問(wèn):.../user/user!add?user.name=a&user.age=1

private User user;

    //private UserDTO userDTO;

    public String add() {

       System.out.println("name=" + user.getName());

       System.out.println("age=" + user.getAge());

 

 

用ModelDriven接收參數(shù):

實(shí)現(xiàn)ModelDriven接口

import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{

//重新方法 

@Override

    public User getModel() {

       return user;

    }

ModelDriven自動(dòng)調(diào)用getModel()方法獲取user對(duì)象

比較常用的是用DomainModel接收參數(shù)

 

 

2.1.6版本的中文問(wèn)題:

<constant name="struts.i18n.encoding" value="GBK" />

在struts.xml文件中配置上面國(guó)際化屬性為GBK使之支持中文

        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

解決不了亂碼問(wèn)題,應(yīng)該使用下面的filter:        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

 

 

簡(jiǎn)單數(shù)據(jù)驗(yàn)證:

    Action:

    public String add() {

       if(name == null || !name.equals("admin")) {

           this.addFieldError("name", "name is error");

           this.addFieldError("name", "name is too long");

           return ERROR;

       }

       return SUCCESS;

    }

    JSP:

    使用struts的標(biāo)簽庫(kù):

    <%@taglib uri="/struts-tags" prefix="s" %>

    <s:fielderror fieldName="name" theme="simple"/>

    <br />

    <s:property value="errors.name[0]"/>

    <s:debug></s:debug>

 

 

訪問(wèn)Web元素:

在Action中取得request的方法:

Map類型:

    Action:

    private Map request;

    private Map session;

    private Map application;

    //可以在構(gòu)造方法中取值

    public LoginAction1() {

       request = (Map)ActionContext.getContext().get("request");

       session = ActionContext.getContext().getSession();

       application = ActionContext.getContext().getApplication();

    }

    public String execute() {

       request.put("r1", "r1");

       session.put("s1", "s1");

       application.put("a1", "a1");

       return SUCCESS;

    }  

    JSP:(兩種訪問(wèn)方法)

        <s:property value="#request.r1"/> | <%=request.getAttribute("r1") %> <br />

    <s:property value="#session.s1"/> | <%=session.getAttribute("s1") %> <br />

    <s:property value="#application.a1"/> | <%=application.getAttribute("a1") %> <br />

public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {

    private Map<String, Object> request;

    private Map<String, Object> session;

    private Map<String, Object> application;

 

    public String execute() {

       request.put("r1", "r1");

       session.put("s1", "s1");

       application.put("a1", "a1");

       return SUCCESS;

    }

原始類型:

    private HttpServletRequest request;

    private HttpSession session;

    private ServletContext application;

    public LoginAction3() {

       request = ServletActionContext.getRequest();

       session = request.getSession();

       application = session.getServletContext();

    }

    JSP:

    <s:property value="#attr.a1"/><br />

    <s:property value="#attr.s1"/><br />

    <s:property value="#attr.r1"/><br />

 

 

模塊包含:

    <include file="login.xml" />

 

 

默認(rèn)Action:

    <default-action-ref name="index"></default-action-ref>

    <action name="index">

        <result>/default.jsp</result>

    </action>

 

 

結(jié)果類型_result_type:

        <action name="r1">

        <result type="dispatcher">/r1.jsp</result>

        </action>

       //dispatcher服務(wù)器跳轉(zhuǎn)到頁(yè)面中取

        <action name="r2">

        <result type="redirect">/r2.jsp</result>

        </action>

       //重定向到頁(yè)面中

        <action name="r3">

        <result type="chain">r1</result>

        </action>

       //forward到另外一個(gè)action

        <action name="r4">

        <result type="redirectAction">r2</result>

        </action>

       重定向到另外一個(gè)action

       

       

Global_Results_全局結(jié)果集:

    <package name="user" namespace="/user" extends="struts-default">

   <global-results>

        <result name="mainpage">/main.jsp</result>

     </global-results>

    ...

</package>

 

    <package name="admin" namespace="/admin" extends="user">

    <action name="admin" class="com.bjsxt.struts2.user.action.AdminAction">

        <result>/admin.jsp</result>

    </action>

    </package>

 

 

動(dòng)態(tài)結(jié)果集_dynamic_result:

    Action:

    public String execute() throws Exception {

       if(type == 1) r="/user_success.jsp";

       else if (type == 2) r="/user_error.jsp";

       return "success";

    }

    struts.xml:

        <action name="user" class="com.bjsxt.struts2.user.action.UserAction">

         <result>${r}</result>

        </action>

    <!--${},OGNL表達(dá)式,表示從值棧中取值-->

 

 

帶參數(shù)的結(jié)果集:

    Action:

private int type;

   

    public int getType() {

       return type;

    }

 

    public void setType(int type) {

       this.type = type;

    }

    struts.xml:

        <result type="redirect">/user_success.jsp?t=${type}</result>

    JSP:

    在URL中指定type的值:

    <a href="user/user?type=1">傳參數(shù)</a>

 

 

OGNL表達(dá)式:

       <li>訪問(wèn)值棧中的action的普通屬性: username = <s:property value="username"/> </li>

       <li>訪問(wèn)值棧中對(duì)象的普通屬性(get set方法):<s:property value="user.age"/> | <s:property value="user['age']"/> | <s:property value="user[\"age\"]"/> | wrong: <%--<s:property value="user[age]"/>--%></li>

//如果user是struts創(chuàng)建,需要保存空的構(gòu)造方法;

       <li>訪問(wèn)值棧中對(duì)象的普通屬性(get set方法): <s:property value="cat.friend.name"/></li>

       <li>訪問(wèn)值棧中對(duì)象的普通方法:<s:property value="password.length()"/></li>

       <li>訪問(wèn)值棧中對(duì)象的普通方法:<s:property value="cat.miaomiao()" /></li>

       <li>訪問(wèn)值棧中action的普通方法:<s:property value="m()" /></li>

       <hr />

//訪問(wèn)靜態(tài)方法需要配置一下常量:

<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>

       <li>訪問(wèn)靜態(tài)方法:<s:property value="@com.bjsxt.struts2.ognl.S@s()"/></li>

       <li>訪問(wèn)靜態(tài)屬性:<s:property value="@com.bjsxt.struts2.ognl.S@STR"/></li>

       <li>訪問(wèn)Math類的靜態(tài)方法:<s:property value="@@max(2,3)" /></li>

       <hr />

       <li>訪問(wèn)普通類的構(gòu)造方法:<s:property value="new com.bjsxt.struts2.ognl.User(8)"/></li>

       <hr />

       <li>訪問(wèn)List:<s:property value="users"/></li>

       <li>訪問(wèn)List中某個(gè)元素:<s:property value="users[1]"/></li>

       <li>訪問(wèn)List中元素某個(gè)屬性的集合:<s:property value="users.{age}"/></li>

//{}在ognl代表一個(gè)集合

       <li>訪問(wèn)List中元素某個(gè)屬性的集合中的特定值:<s:property value="users.{age}[0]"/> | <s:property value="users[0].age"/></li>

       <li>訪問(wèn)Set:<s:property value="dogs"/></li>

       <li>訪問(wèn)Set中某個(gè)元素:<s:property value="dogs[1]"/></li>

// dogs[1]取不出來(lái),因?yàn)閟et元素沒(méi)有順序

       <li>訪問(wèn)Map:<s:property value="dogMap"/></li>

       <li>訪問(wèn)Map中某個(gè)元素:<s:property value="dogMap.dog101"/> | <s:property value="dogMap['dog101']"/> | <s:property value="dogMap[\"dog101\"]"/></li>

       <li>訪問(wèn)Map中所有的key:<s:property value="dogMap.keys"/></li>

       <li>訪問(wèn)Map中所有的value:<s:property value="dogMap.values"/></li>

       <li>訪問(wèn)容器的大?。?lt;s:property value="dogMap.size()"/> | <s:property value="users.size"/> </li>

       <hr />

       <li>投影(過(guò)濾):<s:property value="users.{?#this.age==1}[0]"/></li>

//?代表選擇符合條件的所有元素

       <li>投影:<s:property value="users.{^#this.age>1}.{age}"/></li>

//^代表這些元素中的第一個(gè)

       <li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li>

//^代表這些元素中的最后一個(gè)

       <li>投影:<s:property value="users.{$#this.age>1}.{age} == null"/></li>

       <hr />

       <li>[]:<s:property value="[0].username"/></li>

//[]表示訪問(wèn)值棧中相對(duì)應(yīng)位置的對(duì)象,[0]表示棧頂元素,一般為最近訪問(wèn)的Action

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
structs - 標(biāo)簽庫(kù)(html)
Struts 1.x | <html> 標(biāo)簽庫(kù) - ray - JavaEye技術(shù)網(wǎng)站
歡迎光臨 - 琳婕小筑-老貓的理想 - Struts配置說(shuō)明 -
Struts2 ONGL表達(dá)式
Struts2學(xué)習(xí)自我總結(jié)
Struts標(biāo)簽
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服