Home > Java > Java Tutorial > body text

Share knowledge points about javaee learning

零下一度
Release: 2017-06-25 11:03:00
Original
1707 people have browsed it

1. Result jump method

        /hello.jsp
Copy after login
Forward
        /hello.jsp
Copy after login
Redirect
           Demo1Action  
Copy after login
Forward to Action
          Demo1Action  /
Copy after login
Redirect to Action

二, Access servletAPI method

 1.Principle

 

 2 .Through ActionContext (recommended)

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

 3.Through ServletActionContext

//如何在action中获得原生ServletAPIpublic class Demo6Action extends ActionSupport {//并不推荐public String execute() throws Exception {//原生requestHttpServletRequest request = ServletActionContext.getRequest();//原生sessionHttpSession session = request.getSession();//原生responseHttpServletResponse response = ServletActionContext.getResponse();//原生servletContextServletContext servletContext = ServletActionContext.getServletContext();return SUCCESS;
    }
}
Copy after login
View Code

 4.By implementing the interface

##
//如何在action中获得原生ServletAPIpublic class Demo7Action extends ActionSupport implements ServletRequestAware {    private HttpServletRequest request;public String execute() throws Exception { 
        
        System.out.println("原生request:"+request);return SUCCESS;
    }

    @Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;
    }

}
Copy after login
View Code

 

三、如何获得参数

  1.扩展

    1.1 strutsMVC

        

 

             1.2 Action生命周期

       1.2.1 每次请求到来时,都会创建一个新的Action实例

       1.2.2 Action是线程安全的.可以使用成员变量接收参数

 

  2.属性驱动获得参数

    
用户名:
年龄:
生日:
             
Copy after login
Jsp界面代码
    //准备与参数键名称相同的属性private String name;//自动类型转换 只能转换8大基本数据类型以及对应包装类private Integer age;//支持特定类型字符串转换为Date ,例如 yyyy-MM-ddprivate 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;
    }
Copy after login
后台代码

 

  3.对象驱动

    
用户名:
年龄:
生日:
             
Copy after login
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;
    }
}
Copy after login
后台代码

 

  4.模型驱动

    
用户名:
年龄:
生日:
             
Copy after login
Jsp界面代码
public class Demo10Action extends ActionSupport implements ModelDriven {//准备user 成员变量private User user =new User();public String execute() throws Exception { 
        
        System.out.println(user);        return SUCCESS;
    }

    @Overridepublic User getModel() {return user;
    }
}
Copy after login
后台代码

 

四、集合类型参数封装

  1.List和Map

    
list:
list:
map:
             
Copy after login
Jsp界面代码
public class Demo11Action extends ActionSupport  {//listprivate List list;//Mapprivate Map map;    
    public String execute() throws Exception { 
        
        System.out.println("list:"+list);
        System.out.println("map:"+map);        return SUCCESS;
    }public List getList() {return list;
    }public void setList(List list) {this.list = list;
    }public Map getMap() {return map;
    }public void setMap(Map map) {this.map = map;
    }

}
Copy after login
后台代码

 

五、练习:添加客户

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

     实现步骤:

  

public class CustomerAction extends ActionSupport implements ModelDriven {private CustomerService cs = new CustomerServiceImpl();private Customer customer = new Customer();    //添加客户public String add() throws Exception {//1 调用Service        cs.save(customer);//2 重定向到列表action方法return "toList";
    }
}
Copy after login
主要实现Action代码
    /jsp/customer/list.jsp CustomerAction_list 
Copy after login
struts.xml配置

 

The above is the detailed content of Share knowledge points about javaee learning. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!