登录  /  注册
首页 > Java > java教程 > 正文

分享关于javaee学习的知识点

零下一度
发布: 2017-06-25 11:03:00
原创
1619人浏览过

一、结果跳转方式

        <action><result>/hello.jsp</result></action>
登录后复制
登录后复制
转发
        <action><result>/hello.jsp</result></action>
登录后复制
登录后复制
重定向
        <action> <result> <!-- action的名字 --> <param>Demo1Action <!-- action所在的命名空间 --> <param>/ </result></action>
登录后复制
转发到Action
        <action><result> <!-- action的名字 --> <param>Demo1Action <!-- action所在的命名空间 --> <param>/</result></action>
登录后复制
重定向到Action

 

二、访问servletAPI方式

  1.原理

  

  2.通过ActionContext(推荐)

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

 

  3.通过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;
    }
}
登录后复制
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;
    }

    @Overridepublic 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.属性驱动获得参数

    
登录后复制
登录后复制
登录后复制
登录后复制
用户名:
年龄:
生日:
             
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;
    }
登录后复制
后台代码

 

  3.对象驱动

    
登录后复制
登录后复制
登录后复制
登录后复制
用户名:
年龄:
生日:
             
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.模型驱动

    
登录后复制
登录后复制
登录后复制
登录后复制
用户名:
年龄:
生日:
             
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;
    }

    @Overridepublic User getModel() {return user;
    }
}</user>
登录后复制
后台代码

 

四、集合类型参数封装

  1.List和Map

    
登录后复制
登录后复制
登录后复制
登录后复制
list:
list:
map:
             
Jsp界面代码
public class Demo11Action extends ActionSupport  {//listprivate List<string> list;//Mapprivate Map<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> getMap() {return map;
    }public void setMap(Map<string> map) {this.map = map;
    }

}</string></string></string></string></string></string>
登录后复制
后台代码

 

五、练习:添加客户

  注意: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";
    }
}</customer>
登录后复制
主要实现Action代码
    <package><action><result>/jsp/customer/list.jsp</result><result> <param>CustomerAction_list <param>/ </result></action></package>
登录后复制
struts.xml配置

 

以上就是分享关于javaee学习的知识点的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号