JAVAEE學習——struts2_02:結果跳轉方式、訪問servletAPI方式、獲得參數以及封裝、練習:添加客戶,javaeeservletapi

來源:互聯網
上載者:User

JAVAEE學習——struts2_02:結果跳轉方式、訪問servletAPI方式、獲得參數以及封裝、練習:添加客戶,javaeeservletapi

一、結果跳轉方式

        <action name="Demo1Action" class="cn.itheima.a_result.Demo1Action" method="execute" >            <result name="success" type="dispatcher" >/hello.jsp</result>        </action>
轉寄
        <action name="Demo2Action" class="cn.itheima.a_result.Demo2Action" method="execute" >            <result name="success" type="redirect" >/hello.jsp</result>        </action>
重新導向
        <action name="Demo3Action" class="cn.itheima.a_result.Demo3Action" method="execute" >             <result name="success" type="chain">                     <!-- action的名字 -->                 <param name="actionName">Demo1Action</param>                     <!-- action所在的命名空間 -->                 <param name="namespace">/</param>             </result>        </action>
轉寄到Action
        <action name="Demo4Action" class="cn.itheima.a_result.Demo4Action" method="execute" >            <result  name="success"  type="redirectAction">                 <!-- action的名字 -->                 <param name="actionName">Demo1Action</param>                 <!-- action所在的命名空間 -->                 <param name="namespace">/</param>            </result>        </action>
重新導向到Action

 

二、訪問servletAPI方式

  1.原理

  

  2.通過ActionContext(推薦)

//如何在action中獲得原生ServletAPIpublic class Demo5Action extends ActionSupport {    public String execute() throws Exception {        //request域=> map (struts2並不推薦使用原生request域)        //不推薦        Map<String, Object> requestScope = (Map<String, Object>) ActionContext.getContext().get("request");        //推薦        ActionContext.getContext().put("name", "requestTom");        //session域 => map        Map<String, Object> sessionScope = ActionContext.getContext().getSession();        sessionScope.put("name", "sessionTom");        //application域=>map        Map<String, Object> applicationScope = ActionContext.getContext().getApplication();        applicationScope.put("name", "applicationTom");                return SUCCESS;    }    }
View Code

 

  3.通過ServletActionContext

//如何在action中獲得原生ServletAPIpublic class Demo6Action extends ActionSupport {    //並不推薦    public String execute() throws Exception {        //原生request        HttpServletRequest request = ServletActionContext.getRequest();        //原生session        HttpSession session = request.getSession();        //原生response        HttpServletResponse response = ServletActionContext.getResponse();        //原生servletContext        ServletContext servletContext = ServletActionContext.getServletContext();        return SUCCESS;    }}
View Code

 

  4.通過實現介面方式

//如何在action中獲得原生ServletAPIpublic class Demo7Action extends ActionSupport implements ServletRequestAware {        private HttpServletRequest request;    public String execute() throws Exception {                 System.out.println("原生request:"+request);        return SUCCESS;    }    @Override    public void setServletRequest(HttpServletRequest request) {        this.request = request;    }}
View Code

 

三、如何獲得參數

  1.擴充

    1.1 strutsMVC

        

 

             1.2 Action生命週期

       1.2.1 每次請求到來時,都會建立一個新的Action執行個體

       1.2.2 Action是安全執行緒的.可以使用成員變數接收參數

 

  2.屬性驅動獲得參數

    <form action="${pageContext.request.contextPath}/Demo8Action">        使用者名稱:<input type="text" name="name" /><br>        年齡:<input type="text" name="age" /><br>        生日:<input type="text" name="birthday" /><br>        <input type="submit" value="提交" />    </form>
Jsp介面代碼
    //準備與參數鍵名稱相同的屬性    private String name;    //自動類型轉換 只能轉換8大基礎資料型別 (Elementary Data Type)以及對應封裝類    private Integer age;    //支援特定類型字串轉換為Date ,例如 yyyy-MM-dd    private Date   birthday;        public String execute() throws Exception {                 System.out.println("name參數值:"+name+",age參數值:"+age+",生日:"+birthday);                return SUCCESS;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }
後台代碼

 

  3.對象驅動

    <form action="${pageContext.request.contextPath}/Demo9Action">        使用者名稱:<input type="text" name="user.name" /><br>        年齡:<input type="text" name="user.age" /><br>        生日:<input type="text" name="user.birthday" /><br>        <input type="submit" value="提交" />    </form>
Jsp介面代碼
public class Demo9Action extends ActionSupport  {    //準備user對象    private User user;    public String execute() throws Exception {                 System.out.println(user);                return SUCCESS;    }    public User getUser() {        return user;    }    public void setUser(User user) {        this.user = user;    }}
後台代碼

 

  4.模型驅動

    <form action="${pageContext.request.contextPath}/Demo10Action">        使用者名稱:<input type="text" name="name" /><br>        年齡:<input type="text" name="age" /><br>        生日:<input type="text" name="birthday" /><br>        <input type="submit" value="提交" />    </form>
Jsp介面代碼
public class Demo10Action extends ActionSupport implements ModelDriven<User> {    //準備user 成員變數    private User user =new User();    public String execute() throws Exception {                 System.out.println(user);                return SUCCESS;    }    @Override    public User getModel() {        return user;    }}
後台代碼

 

四、集合型別參數封裝

  1.List和Map

    <form action="${pageContext.request.contextPath}/Demo11Action" method="post" >        list:<input type="text" name="list" /><br>        list:<input type="text" name="list[3]" /><br>        map:<input type="text" name="map['haha']" /><br>        <input type="submit" value="提交" />    </form>
Jsp介面代碼
public class Demo11Action extends ActionSupport  {    //list    private List<String> list;    //Map    private Map<String,String> map;            public String execute() throws Exception {                 System.out.println("list:"+list);        System.out.println("map:"+map);                return SUCCESS;    }    public List<String> getList() {        return list;    }    public void setList(List<String> list) {        this.list = list;    }    public Map<String, String> getMap() {        return map;    }    public void setMap(Map<String, String> map) {        this.map = map;    }}
後台代碼

 

五、練習:添加客戶

  注意:struts和hibernate包在合并時.javassist-3.18.1-GA.jar包是重複的,刪除版本低的.

     實現步驟:

  

public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {    private CustomerService cs = new CustomerServiceImpl();    private Customer customer = new Customer();        //添加客戶    public String add() throws Exception {        //1 調用Service        cs.save(customer);        //2 重新導向到列表action方法        return "toList";    }}
主要實現Action代碼
    <package name="crm" namespace="/" extends="struts-default" >        <action name="CustomerAction_*" class="cn.itheima.web.action.CustomerAction" method="{1}" >            <result name="list" >/jsp/customer/list.jsp</result>            <result name="toList" type="redirectAction">                 <param name="actionName">CustomerAction_list</param>                 <param name="namespace">/</param>             </result>        </action>    </package>
struts.xml配置

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.