您当前的位置: 首页 >  ssh

xiangzhihong8

暂无认证

  • 3浏览

    0关注

    1324博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ssh框架搭建

xiangzhihong8 发布时间:2014-04-16 13:26:37 ,浏览量:3

目录结构:

一、新建web project

二、在src目录下新建struts.xml  ,使用include是为了方便分开各个action

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1.   
  2.   
  3.   
  4.   
  5.       
  6.       
  7.       
  8.       
  9.       
  10.       
  11.       
  12.       
  13.       
  14.   
  15.   
还有admin.xml
[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1.   
  2.   
  3.   
  4.   
  5.       
  6.          
  7.                        
  8.                /backend/main.jsp     
  9.               
  10.                  
  11.                admin_login   
  12.               
  13.               
  14.           
  15.       
  16.   

三、在src目录增加hibernate.cfg.xml

[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1.   
  2.   
  3.   
  4.   
  5.   
  6.   
  7.       
  8.           
  9.   
  10.       
  11.   
  12.   
四、在WEB-INF增加applicationContext.xml
[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1.   
  2.   
  3.   
  4.       
  5.           
  6.           
  7.             com.mysql.jdbc.Driver  
  8.            
  9.           
  10.           
  11.             jdbc:mysql://localhost:3306/pacs  
  12.           
  13.           
  14.           
  15.             root  
  16.           
  17.           
  18.           
  19.             root  
  20.           
  21.           
  22.           
  23.             20  
  24.           
  25.           
  26.           
  27.             2  
  28.           
  29.           
  30.           
  31.             2  
  32.           
  33.           
  34.           
  35.             20  
  36.           
  37.       
  38.       
  39.           
  40.               
  41.           
  42.               
  43.                   
  44.                  org.hibernate.dialect.MySQLDialect     
  45.                  true     
  46.                  true   
  47.                  true     
  48.                  auto     
  49.                  true     
  50.                  true  
  51.                  true  
  52.                  org.hibernate.cache.EhCacheProvider   
  53.                  
  54.                   
  55.             
  56.           
  57.               
  58.                 com/d3/pacs/model/Admin.hbm.xml           
  59.               
  60.           
  61.       
  62.       
  63.       
  64.       
  65.           
  66.       
  67.       
  68.       
  69.       
  70.       
  71.       
  72.       
  73.           
  74.       
  75.       
  76.           
  77.       
  78.       
  79.   
五、web.xml需要修改
[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1.   
  2.   
  3.      
  4.       
  5.       
  6.         struts2  
  7.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter  
  8.       
  9.       
  10.         struts2  
  11.         /*  
  12.       
  13.   
  14.       
  15.       
  16.         org.springframework.web.context.ContextLoaderListener  
  17.       
  18.       
  19.     
  20.     login.jsp  
  21.     
  22.   

六、编写action

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class AdminAction extends ActionSupport {  
  2.     private static Logger logger = Logger.getLogger(AdminAction.class);  
  3.     private AdminService adminService;  
  4.     private Admin admin;  
  5.   
  6.     public AdminService getAdminService() {  
  7.         return adminService;  
  8.     }  
  9.   
  10.     public void setAdminService(AdminService adminService) {  
  11.         this.adminService = adminService;  
  12.     }  
  13.   
  14.     public Admin getAdmin() {  
  15.         return admin;  
  16.     }  
  17.   
  18.     public void setAdmin(Admin admin) {  
  19.         this.admin = admin;  
  20.     }  
  21.   
  22.     // 管理员登陆  
  23.     public String login() {  
  24.         if(adminService.login(admin) !=null){  
  25.             HttpServletRequest request = ServletActionContext.getRequest();  
  26.             HttpSession session = request.getSession(true);  
  27.             session.setAttribute("admin1", admin);  
  28.             return "login";  
  29.         }  
  30.         else  
  31.             return "loginFail";  
  32.     }  
  33.   
  34.     // 管理员注册  
  35.     public String regist() {  
  36.         return "regist";  
  37.     }  
  38.   
  39.     // 退出管理员  
  40.     public String exit() {  
  41.         HttpServletRequest request = ServletActionContext.getRequest();  
  42.         request.getSession().removeAttribute("admin1");  
  43.         return "exit";  
  44.     }  
  45. }  
七、   访问流程: 在jsp中提交action    提交表单    

/my是我的tomcat自己定义的虚拟目录,   admin_* 对应的是admin.xml中 

访问admin_login  对应的就是action类中的public String login()的方法 ,根据此方法返回的String 

对应的也是admin.xml 中 ,返回login则会跳刀main.jsp

                      /backend/main.jsp                                                   /backend/main.jsp                                             admin_login            

另外,struts会自动匹配类属性和jsp中name

比如: 

jsp里面有  :  

对应在action里面,如果有name的属性,只要提供get,set方法,那么jsp中的name就会传到action

另外还可以:    

对应action里面有个admin的类属性且 admin有name属性, 则会赋值

下载地址:  SSH框架模板,已搭建完毕  点击下载

关注
打赏
1482932726
查看更多评论
立即登录/注册

微信扫码登录

0.0709s