第二章 Struts 2的应用

2.1 Struts 2的应用
    2.1.1 使用步骤
        1、创建web项目,添加jar包,创建helloWorld.jsp页面
        2、创建HelloWorldAction类,用于对用户的请求作出处理
            public class HelloWorld implements Action {
                //用户输入的姓名
                private String name = "";
                //向用户显示的信息
                private String message = "";
                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;
                }
                @Override
                public String execute() throws Exception {
                    this.setMessage("Hello,"+this.getName()+"!");
                    return "success";
                }
            }

        3、修改项目配置文件web.xml,将所有请求定位到指定当的Struts 2 过滤器中
              <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>
        4、创建Struts 2配置文件
            Struts.xmll:
                <?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>
                    <constant name="struts.ui.theme" value="simple"></constant>
                    <package name="default" namespace="/" extends="struts-default">
                        <action name="helloWorld" class="cn.jbit.strutsdemo.HelloWorld">
                            <result name="success">helloWorld.jsp</result>
                        </action>
                        <action name="login" class="cn.jbit.strutsdemo.loginAction">
                            <result name="loginInfo">loginInfo.jsp</result>
                            <result name="login">login.jsp</result>
                            <result name="input">login.jsp</result>
                        </action>

                    </package>
                </struts>
        总结步骤:
            1、确认环境
                1、将Struts 2框架支持文件引入项目
                2、修改工程的web.xml文件,配置过滤器            
            2、代码编写
                1、编写开发处理求求的Action类,实现处理请求的方法,返回一个字符串类型的结果
                2、编写Struts.xml文件,对Action进行配置
                3、编写与Action相关的JSP页面
2.2 Struts 2访问servletAPI对象
    1、使用ActionContex类获取ServletPAI对应的Map对象
        request:ActionContext ac=ActionContext.getContext();
                 Map request=(Map)ac.get("request");
        session: ActionContext ac=ActionContext.getContext();
                 Map session=(Map)ac.get("session");
        application:ActionContext ac=ActionContext.getContext();
                 Map application=(Map)ac.get("application");      
    2、Struts 2 向Action注入ServletAPI对象对应的Map对象
        org.apache.struts2.interceptor.RequestAware:向Action实例注入HttpServletRequest对象对应的Map对象
            public void setRequest(Map<String,Object>request);
            
        org.apache.struts2.interceptor.SessionAware:向Action实例注入HttpSession对象对应的Map对象
            public void setSession(Map<String,Object>Session);
            
        org.apache.struts2.interceptor.ApplicationAware:向Action实例注入ServletContext对象对应的Map对象
            public void setApplication(Map<String,Object>Application);
    与Servlet API耦合的访问方式
        使用ServletActionContext类获取ServletAPI对象
        
            HttpServletRequest  =  public static HttpServletRequest getRequest();
                  
            ServletCOntext  =  public static ServletContext getServletContext();      
                  
            HttpServletResponse  =  public static HttpServletResponse getResponse();      
                  
            使用HttpServletRequest可以获得HttpSession对象
        除了使用ServletActionContext获取ServletAPI对象,还可以实现特定的接口实现注入Action实例注入ServletAPI对象
            ServletContext   :void setServletCOntext(ServletCOntext context);
            
            HttpServletRequest   :void serServletRequest(HttpServletRequest request);
            
            HttpServletResponse   :void setServletResponse(HttpServletResponse response);  
2.3 Struts 2数据校验
        通过继承ActionSupport类实现数据验证机制
        例:    public void validate(){
                    if(this.getUsername().length()==0||"".equals(this.getUsername())){
                        addFieldError("name", "用户名不能为空");
                    }
                    if(this.getPassword().length()==0||"".equals(this.getPassword())){
                        addFieldError("pwd", "密码不能为空");
                    }
                }
2.4 Struts 2标签
    UI标签
        表单标签:
            <s:form></s:form>:获取相应form的值
            <s:textfied></s:textfied>:文本输入框
            <s:password></password>:密码输入框
            <s:textarea></textarea>:文本域输入框
            <s:radio></s:radio>:单选按钮
            <s:checkbox></s:checkbox>:复选框
            <s:submit/>:提交标签
            <s:reset/>:重置标签
            <s:hidden/>:隐藏域标签
            例:<s:form action="login.action">
                    <div>用户名:<s:textfield name="username"/></div>
                    <div>密码:<s:password name="password"/><br></div>
                    <div><s:submit value="提交"/></div>
                </s:form>
                <font color="red"><s:property value="text"/><s:fielderror/></font>
                
        非表单标签
        Ajax标签
    通用标签
          条件标签:
          <s:if>
            需执行的代码
          </s:if>
          <s:elseif>
            需执行的代码
          </s:elseif>
          <s:else>
            需执行的代码
          </s:else>
          迭代标签:<s:iterator value="集合对象" status="status" id="name">
                        读取集合对象并输出显示
                    </s:iterator>
            

posted @ 2016-11-23 11:59  LuoBaiLin  阅读(124)  评论(0编辑  收藏  举报