一、使用struts2实现文件上传 1、写页面upload.jsp 2、写uploadAction.java文件
private File myfile; private String myfileFileName; public String upload() throws IOException{ String path=''D:/upload''; File f=new File(path); if(!f.exists()){ f.mkdirs(); } File file=new File(path,myfileFileName); FileUtils.copyFile(myfile,file); return ''upload''; } 3、在struts.xml文件中配置 二、使用struts2框架和ajax写用户名校验 1、写ajax.jsp页面 2、写ajaxAction.java文件 private String name; private String msg; public String ajax() throws IOException{ if(name.equals(''小明'')){ msg=''1''; }else{ msg=''0''; } HttpServletResponse response= ServletActionContext.getResponse(); response.setCharacterEncoding(''utf-8''); response.getWriter().print(msg); return null; } 3、在xml文件中配置 三、使用struts2框架通过hello.action路径,可以访问到TestAction的hello()方法
四、采用hibernate框架编写代码对people表进行增,删,改,查操作 用hibernate框架首先写hibernate.cfg.xml配置文件 1、写pojo类people.java
@Entitypublic class People { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; private String name; private Integer age; private String sex; 2、写测试类测试 public void save(){ Session session=HibernateUtil.getSessionfactory().openSession(); Transaction ta=session.beginTransaction(); session.save(new People(null, ''小芳'', 26, ''男'')); ta.commit(); session.close(); } public void delete(){ Session session=HibernateUtil.getSessionfactory().openSession(); Transaction ta=session.beginTransaction(); People people=session.get(People.class, 1); session.delete(people); ta.commit(); session.close(); } public void update(){ Session session=HibernateUtil.getSessionfactory().openSession(); Transaction ta=session.beginTransaction(); Query query=session.createQuery(''update People set age=age+1''); query.executeUpdate(); ta.commit(); session.close(); } public void sel(){ Session session=HibernateUtil.getSessionfactory().openSession(); Transaction ta=session.beginTransaction(); People people=session.get(People.class, 2); System.out.println(people); ta.commit(); session.close(); } public static void main(String[] args) { PeopleTest test=new PeopleTest(); //test.save(); //test.delete(); //test.update(); test.sel(); } 五、通过hibernate框架描述emp表和dept表的一对多关系,并写主方法查询,在查询部门信息的同时,把该部门下的员工信息查出来 1、emp.java与dept.java文件 emp:@Entitypublic class Emp { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; private String name;dept:@Entitypublic class Dept { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; private String name; @OneToMany @JoinColumn(name=''dept_id'') private Set 2、写测试类 public void save(){ Session session=HibernateUtil.getSessionfactory().openSession(); Transaction ta=session.beginTransaction(); Set
|
|