一,、使用struts2實(shí)現(xiàn)文件上傳 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寫用戶名校驗(yàn) 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框架編寫代碼對(duì)people表進(jìn)行增,刪,改,查操作 用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,、寫測(cè)試類測(cè)試 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表的一對(duì)多關(guān)系,,并寫主方法查詢,,在查詢部門信息的同時(shí),把該部門下的員工信息查出來 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,、寫測(cè)試類 public void save(){ Session session=HibernateUtil.getSessionfactory().openSession(); Transaction ta=session.beginTransaction(); Set
|
|