struts2----摘要2

    技术2022-06-12  45

    1.

    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;

        }

     

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

        <br />

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

        <br/>

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

        <s:debug></s:debug>

     

    2.

     

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

       

        private Map<String, Object> request;

        private Map<String, Object> session;

        private Map<String, Object> application;

       

        //DI dependency injection

        //IoC inverse of control

        public String execute() {

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

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

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

           return SUCCESS;

        }

     

        @Override

        public void setRequest(Map<String, Object> request) {

           this.request = request;

        }

     

        @Override

        public void setSession(Map<String, Object> session) {

           this.session = session;

        }

     

        @Override

        public void setApplication(Map<String, Object> application) {

           this.application = application;

        }

       

    }

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

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

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

     

     

     

    3.

    <struts>

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

        <include file="login.xml" />

    </struts>

    4.

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

    5.

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

           <global-results>

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

           </global-results>

       

            <action name="index">

                <result>/index.jsp</result>

            </action>

       

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

               <result>/user_success.jsp</result>

                <result name="error">/user_error.jsp</result>

    </action>     

    </package>

     

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

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

                <result>/admin.jsp</result>

           </action>

     

        </package>

    6.

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

            <result>${r}</result>

        </action> 

     

    private String r;

     

        public String getR() {

           return r;

        }

     

        public void setR(String r) {

           this.r = r;

        }

       public String execute() throws Exception {

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

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

           return "success";

        }

     

     

     

     

     

     

    7.

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

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

    </action>   

     

    from valuestack: <s:property value="t"/><br/>

       from actioncontext: <s:property value="#parameters.t"/>


    最新回复(0)