1、写页面upload.jsp
2、写uploadAction.java文件
1、获取myfile,myfileFileName的get与set方法
2、指定路径
3、判断路径是否存在,如果不存在则新建
4、把文件名与路径拼接
5、使用fileUtiles工具类把文件复制到指定路径下
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文件中配置
/uploadsuccess.jsp upload 二、使用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()方法
在xml文件中添加代码
四、采用hibernate框架编写代码对people表进行增,删,改,查操作
用hibernate框架首先写hibernate.cfg.xml配置文件
com.mysql.jdbc jdbc:mysql://localhost:3306/work_hibernate root wanghui true true update org.hibernate.dialect.MySQL5InnoDBDialect 4 1、写pojo类people.java
@Entity创建表
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
指定主键与主键生成策略
@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 emps=new HashSet<>();
2、写测试类
public void save(){ Session session=HibernateUtil.getSessionfactory().openSession(); Transaction ta=session.beginTransaction(); Set emps=new HashSet<>(); emps.add(session.get(Emp.class, 1)); session.save(new Dept(null, ''研发部'',emps)); ta.commit(); session.close(); } public void sel(){ Session session=HibernateUtil.getSessionfactory().openSession(); Transaction ta=session.beginTransaction(); Dept dept=session.get(Dept.class, 1); System.out.println(dept); ta.commit(); session.close(); } public static void main(String[] args) { DeptTest test=new DeptTest(); //test.save(); test.sel(); }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。