使用ADSI实现IIS管理,WEB站点管理系统核心代码

简介: 代码 using System; using System.Collections.Generic; using System.Text; using System.IO; using System.
img_405b18b4b6584ae338e0f6ecaf736533.gif 代码
using  System; 
using  System.Collections.Generic; 
using  System.Text; 
using  System.IO; 
using  System.Collections; 
using  System.DirectoryServices; 
using  IISMModel; 
using  System.Configuration; 
using  System.Web; 
 
namespace  IISMBLL 

    
///   <summary>  
    
///  IIS站点管理器 
    
///   </summary>  
     public   class  IISManager 
    { 
        
const   string  W3SVC_PATH  =   " IIS://localhost/W3SVC "
        
//  Access Flags 
         const   int  MD_ACCESS_READ  =   0x00000001 // Allow read access. 
         const   int  MD_ACCESS_SCRIPT  =   0x00000200 //  Allow script execution. 
 
        
///   <summary>  
        
///  验证某个虚拟目录是否存在 
        
///   </summary>  
        
///   <param name="baseSiteName"> 虚拟目录所在的基础站点 </param>  
        
///   <param name="virtualDirName"> 虚拟目录名 </param>  
        
///   <returns> 存在返回真,否则返回假 </returns>  
         public  Boolean ValidateVirWebSite( string  baseSiteName, string  virtualDirName) 
        { 
            
if  ( string .IsNullOrEmpty(baseSiteName)) 
            { 
                
throw   new  Exception(IISModel_MSG.IISBLL_IISM_ERROR_VIRDIR_SITE_EMPTY); 
            } 
 
            
if  ( string .IsNullOrEmpty(virtualDirName)) 
            { 
                
throw   new  Exception(IISModel_MSG.IISBLL_IISM_ERROR_VIRDIR); 
            } 
 
            
//  站点ID 
             int  _WebSiteID  =   1
            _WebSiteID 
=  GetSiteID(baseSiteName,  " localhost " ); 
            
if  (_WebSiteID  ==   0
            { 
                
throw   new  Exception(IISModel_MSG.IISBLL_IISM_ERROR_VIRDIR_SITE_NOTFOUND); 
            } 
 
            String siteRoot 
=   " IIS://localhost/W3SVC/ "   +  _WebSiteID.ToString()  +   " /ROOT "
            DirectoryEntry root 
=   new  DirectoryEntry(siteRoot); 
            
foreach  (DirectoryEntry child  in  root.Children) 
            { 
                
if  (child.Name.ToLower()  ==  virtualDirName) 
                { 
                    
return   true
                } 
            } 
            
return   false
        } 
 
        
///   <summary>  
        
///  验证某个站点是否存在 
        
///   </summary>  
        
///   <param name="siteName"></param>  
        
///   <returns></returns>  
         public  Boolean ValidateWebSite( string  siteName) 
        { 
            
return  
                GetSiteID(siteName, 
" localhost " ) > 0
        } 
 
        
///   <summary>  
        
///  在主机上创建虚拟目录 
        
///   </summary>  
         public   void  CreateVirWebSite( string  _virtualDirName,  string  _physicalPath) 
        { 
            
try  
            { 
                
//  站点ID 
                 int  _WebSiteID  =   1
                
// 得到默认站点 
                _WebSiteID  =  GetSiteID(ConfigurationManager.AppSettings[ " defaultSite " ],  " localhost " ); 
                
if  (_WebSiteID  ==   0
                { 
                    _WebSiteID 
=   1
                } 
                
if  ( string .IsNullOrEmpty(_virtualDirName)) 
                { 
                    
throw   new  NullReferenceException( " 创建虚拟目录名不能为空 " ); 
                } 
 
                String constIISWebSiteRoot 
=   " IIS://localhost/W3SVC/ "   +  _WebSiteID.ToString()  +   " /ROOT "
                
string  virtualDirName  =  _virtualDirName;  // 虚拟目录名称:Monitor 
                 string  physicalPath  =  _physicalPath;  // 虚拟目录物理路径 
 
                
if  (String.IsNullOrEmpty(physicalPath)) 
                { 
                    
throw   new  NullReferenceException( " 创建虚拟目录的物理路径不能为空 " ); 
                } 
 
                DirectoryEntry root 
=   new  DirectoryEntry(constIISWebSiteRoot); 
 
                
//  如果虚拟目录存在则删除 
                 foreach  (System.DirectoryServices.DirectoryEntry v  in  root.Children) 
                { 
 
                    
if  (v.Name  ==  _virtualDirName) 
                    { 
                        
//  Delete the specified virtual directory if it already exists 
                         try  
                        { 
                            root.Invoke(
" Delete " new   string [] { v.SchemaClassName, _virtualDirName }); 
                            root.CommitChanges(); 
                        } 
                        
catch  
                        { 
                            
throw ;                             
                        } 
                    } 
                } 
 
                DirectoryEntry tbEntry 
=  root.Children.Add(virtualDirName,  " IIsWebVirtualDir " ); 
                 
                tbEntry.Properties[
" Path " ][ 0 =  physicalPath;   // 虚拟目录物理路径 
                tbEntry.Invoke( " AppCreate " true ); 
                tbEntry.Properties[
" AccessRead " ][ 0 =   true ;    // 设置读取权限 
                tbEntry.Properties[ " ContentIndexed " ][ 0 =   true
                tbEntry.Properties[
" DefaultDoc " ][ 0 =   " index.aspx,Default.aspx " // 设置默认文档,多值情况下中间用逗号分割 
                tbEntry.Properties[ " AppFriendlyName " ][ 0 =  virtualDirName;  // 虚拟目录名称:Monitor 
                tbEntry.Properties[ " AccessScript " ][ 0 =   true // 执行权限 
                tbEntry.Properties[ " AppIsolated " ][ 0 =   " 1 "
                tbEntry.Properties[
" DontLog " ][ 0 =   true ;   //  是否记录日志                 
                tbEntry.Properties[ " AuthFlags " ][ 0 =   1 ; //  设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证 
                tbEntry.CommitChanges(); 
 
                
//  增加MIME 的类型:flv 
                
// SetMimeTypeProperty(constIISWebSiteRoot, ".flv", "flv"); 
 
                
//  修改IIS属性 
                
// SetSingleProperty("IIS: // localhost/W3SVC/" + _WebSiteID.ToString(), "ServerBindings", ":8080:"); 
 
                
/// /得到现默认站点的IP 端口 描述        
                 // string strServerBindings = root.Properties["ServerBindings"].Value.ToString(); 
                 /// /解出端口Port 
                 // string[] strArr = strServerBindings.Split(':'); 
                 /// /重新赋值为8080 
                 // root.Properties["ServerBindings"].Value = strArr[0] + ":8080:" + strArr[2]; 
 
                root.CommitChanges();
// 确认更改 
 
            } 
            
catch  
            { 
                 
            } 
        } 
 
        
///   <summary>  
        
///  删除Web虚拟目录 
        
///   </summary>  
        
///   <param name="_virtualDirName"></param>  
         public   void  RemoveVirWebSite( string  _virtualDirName) 
        { 
            
//  站点ID 
             int  _WebSiteID  =   1
            
// 得到默认站点 
            _WebSiteID  =  GetSiteID(ConfigurationManager.AppSettings[ " defaultSite " ],  " localhost " ); 
            
if  (_WebSiteID  ==   0
            { 
                _WebSiteID 
=   1
            } 
            
if  ( string .IsNullOrEmpty(_virtualDirName)) 
            { 
                
throw   new  NullReferenceException( " 创建虚拟目录名不能为空 " ); 
            } 
 
            String constIISWebSiteRoot 
=   " IIS://localhost/W3SVC/ "   +  _WebSiteID.ToString()  +   " /ROOT "
            
string  virtualDirName  =  _virtualDirName;  // 虚拟目录名称:Monitor 
 
            
using  (DirectoryEntry root  =   new  DirectoryEntry(constIISWebSiteRoot)) 
            { 
                
//  如果虚拟目录存在则删除 
                 foreach  (System.DirectoryServices.DirectoryEntry v  in  root.Children) 
                { 
                    
if  (v.Name  ==  _virtualDirName) 
                    { 
                        root.Invoke(
" Delete " new   string [] { v.SchemaClassName, _virtualDirName }); 
                        root.CommitChanges(); 
                        
break
                    } 
                } 
                root.Close();  
            } 
        } 
 
        
///   <summary>  
        
///  创建独立的Web站点 
        
///   </summary>  
        
///   <param name="siteName"></param>  
        
///   <param name="realPath"></param>  
         public   void  CreateWebSite( string  siteName,  string  realPath) 
        { 
            
if  ( string .IsNullOrEmpty(siteName)) 
            { 
                
throw   new  NullReferenceException( " 创建站点的站点名字不能为空 " ); 
            } 
            
if  ( string .IsNullOrEmpty(realPath)) 
            { 
                
throw   new  NullReferenceException( " 创建站点的站点真是路径不能为空 " ); 
            } 
 
            DirectoryEntry root 
=   new  DirectoryEntry(W3SVC_PATH); 
 
            
// 判断站点是否已经存在,如果存在,则删除已有站点 
             int  siteID  =  GetSiteID(siteName,  " localhost " ); 
            
if  (siteID  >   0
            { 
                
throw   new  Exception( " 要创建的站点已经存在 " ); 
            } 
 
            siteID 
=  getNewWebSiteID(); 
            DirectoryEntry currentWeb 
=  
                root.Children.Add(siteID.ToString(), 
" IIsWebServer " ); 
            currentWeb.CommitChanges(); 
 
            
// currentWeb.Properties["Location"].Value = "/LM/W3SVC/" + siteID.ToString(); 
            currentWeb.Properties[ " AuthFlags " ][ 0 =   " 0 "
            currentWeb.Properties[
" MaxBandwidth " ][ 0 =   " 1048576 "
            currentWeb.Properties[
" MaxConnections " ][ 0 =   " 10 "
            currentWeb.Properties[
" ServerAutoStart " ][ 0 =   " true "
            currentWeb.Properties[
" ServerBindings " ].Value  =   " :80: "   +  siteName; 
            currentWeb.Properties[
" ServerComment " ][ 0 =  siteName; 
             
            
//  添加web虚拟目录 
            DirectoryEntry virEntry  =  currentWeb.Children.Add( " root " , " IIsWebVirtualDir " ); 
            virEntry.CommitChanges(); 
            
// virEntry.Properties["Location"].Value = "/LM/W3SVC/"+siteID.ToString()+"/root"; 
            virEntry.Properties[ " AccessFlags " ][ 0 =  MD_ACCESS_READ  |  MD_ACCESS_SCRIPT; 
            virEntry.Properties[
" AppFriendlyName " ][ 0 =  siteName; 
            virEntry.Properties[
" AppIsolated " ][ 0 =   " 2 "
            virEntry.Properties[
" AppRoot " ][ 0 =   " /LM/W3SVC/ "   +  siteID.ToString()  +   " /Root "
            virEntry.Properties[
" AuthFlags " ][ 0 =   1   |   7 ; //  设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证             
            virEntry.Properties[ " Path " ][ 0 =  realPath;   // 虚拟目录物理路径 
             
            virEntry.CommitChanges(); 
            currentWeb.CommitChanges(); 
 
            virEntry.Close(); 
            currentWeb.Close(); 
            root.Close(); 
        } 
 
        
///   <summary>  
        
///  删除Web站点 
        
///   </summary>  
        
///   <param name="siteName"></param>  
         public   void  RemoveWebSite( string  siteName) 
        { 
            
if  ( string .IsNullOrEmpty(siteName)) 
            { 
                
throw   new  NullReferenceException( " 创建站点的站点名字不能为空 " ); 
            } 
 
            
using  (DirectoryEntry root  =   new  DirectoryEntry(W3SVC_PATH)) 
            { 
                
foreach  (DirectoryEntry Child  in  root.Children) 
                { 
                    
if  (Child.SchemaClassName  ==   " IIsWebServer "
                    { 
                        
string  WName  =  Child.Properties[ " ServerComment " ][ 0 ].ToString(); 
                        
if  (siteName.ToLower()  ==  WName.ToLower()) 
                        { 
                            Child.DeleteTree(); 
                            root.CommitChanges(); 
                            
break
                        }   
                    } 
                                       
                } 
                root.Close(); 
            } 
 
             
        } 
 
        
///   <summary>  
        
///  得到新的站点ID 
        
///   </summary>  
        
///   <returns></returns>  
         private   int  getNewWebSiteID() 
        { 
           
using (System.DirectoryServices.DirectoryEntry rootEntry  =   
               
new  System.DirectoryServices.DirectoryEntry(W3SVC_PATH)) 
           { 
               
int  siteID  =   1
               
// 得到现有的站点标识 
                foreach  (System.DirectoryServices.DirectoryEntry entry  in  rootEntry.Children) 
               { 
                   
if  (entry.SchemaClassName  ==   " IIsWebServer "
                   { 
                       
int  ID  =  Convert.ToInt32(entry.Name); 
 
                       
if  (ID  >=  siteID) 
                       { 
                           siteID 
=  ID  +   1
                       } 
                   } 
               } 
               rootEntry.Close(); 
               
return  siteID; 
           } 
        } 
 
        
#region  根据站点名称获取站点ID(标识) 
 
        
///   <summary>  
        
///  根据站点名称获取站点ID(标识) 
        
///   </summary>  
        
///   <param name="webSiteName"> 站点名称 </param>  
        
///   <param name="serverIP"> 主机IP </param>  
        
///   <returns> 站点ID </returns>  
         private   int  GetSiteID( string  webSiteName,  string  serverIP) 
        { 
            
int  SiteID  =   0
            
try  
            { 
                DirectoryEntry root 
=   new  DirectoryEntry( @" IIS:// "   +  serverIP  +   " /W3SVC " );                 
                
foreach  (DirectoryEntry Child  in  root.Children) 
                {                     
                    
string  WName  =  Child.Properties[ " ServerComment " ][ 0 ].ToString(); 
                    
if  (webSiteName  ==  WName) 
                    {                         
                        SiteID 
=  Convert.ToInt32(Child.Name); 
                        
return  SiteID; 
                    } 
                    WName 
=   ""
                } 
            } 
            
catch  
            { 
                SiteID 
=   0
            } 
            
return  SiteID; 
        } 
 
        
#endregion  
 
        
///   <summary>  
        
///  设置IIS的MIME类型,SetMimeTypeProperty("IIS://localhost/W3SVC/1/Root", ".hlp", "application/winhlp"); 
        
///   </summary>  
        
///   <param name="metabasePath"></param>  
        
///   <param name="newExtension"></param>  
        
///   <param name="newMimeType"></param>  
         static   void  SetMimeTypeProperty( string  metabasePath,  string  newExtension,  string  newMimeType) 
        { 
            
//   metabasePath is of the form "IIS: // <servername>/<path>" 
            
//     for example "IIS: // localhost/W3SVC/1/Root"  
            
//   newExtension is of the form ".<extension>", for example, ".hlp" 
            
//   newMimeType is of the form "<application>", for example, "application/winhlp" 
            Console.WriteLine( " \nAdding {1}->{2} to the MimeMap property at {0}: " , metabasePath, newExtension, newMimeType); 
 
            
try  
            { 
                DirectoryEntry path 
=   new  DirectoryEntry(metabasePath); 
                PropertyValueCollection propValues 
=  path.Properties[ " MimeMap " ]; 
                Console.WriteLine(
"  Old value of MimeMap has {0} elements " , propValues.Count); 
 
                
object  exists  =   null
                
foreach  ( object  value  in  propValues) 
                { 
                    
//  IISOle requires a reference to the Active DS IIS Namespace Provider in Visual Studio .NET 
                    IISOle.IISMimeType mimetypeObj  =  (IISOle.IISMimeType)value; 
                    Console.WriteLine(
"   {0}->{1} " , mimetypeObj.Extension, mimetypeObj.MimeType); 
                    
if  (newExtension  ==  mimetypeObj.Extension) 
                        exists 
=  value; 
                } 
 
                
if  ( null   !=  exists) 
                { 
                    propValues.Remove(exists); 
                    Console.WriteLine(
"  Found an entry for {0}; removing it before adding the new one. " , newExtension); 
                } 
 
                IISOle.MimeMapClass newObj 
=   new  IISOle.MimeMapClass(); 
                newObj.Extension 
=  newExtension; 
                newObj.MimeType 
=  newMimeType; 
                propValues.Add(newObj); 
                path.CommitChanges(); 
                Console.WriteLine(
"  Done. " ); 
 
            } 
            
catch  (Exception ex) 
            { 
                
if  ( " HRESULT 0x80005006 "   ==  ex.Message) 
                    Console.WriteLine(
"  Property MimeMap does not exist at {0} " , metabasePath); 
                
else  
                    Console.WriteLine(
" Failed in SetMimeTypeProperty with the following exception: \n{0} " , ex.Message); 
            } 
        } 
 
        
///   <summary>  
        
///  修改IIS属性 
        
///   </summary>  
        
///   <param name="metabasePath"></param>  
        
///   <param name="propertyName"></param>  
        
///   <param name="newValue"></param>  
         static   void  SetSingleProperty( string  metabasePath,  string  propertyName,  object  newValue) 
        { 
            
//   metabasePath is of the form "IIS: // <servername>/<path>" 
            
//     for example "IIS: // localhost/W3SVC/1"  
            
//   propertyName is of the form "<propertyName>", for example "ServerBindings" 
            
//   value is of the form "<intStringOrBool>", for example, ":80:" 
            Console.WriteLine( " \nSetting single property at {0}/{1} to {2} ({3}): "
                metabasePath, propertyName, newValue, newValue.GetType().ToString()); 
 
            
try  
            { 
                DirectoryEntry path 
=   new  DirectoryEntry(metabasePath); 
                PropertyValueCollection propValues 
=  path.Properties[propertyName]; 
                
string  oldType  =  propValues.Value.GetType().ToString(); 
                
string  newType  =  newValue.GetType().ToString(); 
                Console.WriteLine(
"  Old value of {0} is {1} ({2}) " , propertyName, propValues.Value, oldType); 
                
if  (newType  ==  oldType) 
                { 
                    path.Properties[propertyName][
0 =  newValue; 
                    path.CommitChanges(); 
                    Console.WriteLine(
" Done " ); 
                } 
                
else  
                    Console.WriteLine(
"  Failed in SetSingleProperty; type of new value does not match property " ); 
            } 
            
catch  (Exception ex) 
            { 
                
if  ( " HRESULT 0x80005006 "   ==  ex.Message) 
                    Console.WriteLine(
"  Property {0} does not exist at {1} " , propertyName, metabasePath); 
                
else  
                    Console.WriteLine(
" Failed in SetSingleProperty with the following exception: \n{0} " , ex.Message); 
            } 
        } 
         
        
///   <summary>  
        
///  创建FTP虚拟目录 
        
///   </summary>  
        
///   <param name="virDirName"></param>  
        
///   <param name="realPath"></param>  
         public   void  CreateFTPVirDir( string  virDirName, string  realPath) 
        { 
              
string    strSchema =     " IIsFtpVirtualDir " ;    
              
string    strRootSubPath    =     " /MSFTPSVC/1/Root "
 
              
string  FtpName  =  virDirName; 
              
string  DirName  =  realPath;    
                 
              DirectoryEntry   deRoot
=     new    DirectoryEntry( " IIS://localhost " +    strRootSubPath); 
 
              
try  
              { 
                  deRoot.RefreshCache();    
                  DirectoryEntry   deNewVDir   
=    deRoot.Children.Add(FtpName,strSchema);    
                     
                  
// deNewVDir.Properties["Path"].Insert(0,DirName); 
                  deNewVDir.Properties[ " Path " ][ 0 =  DirName; 
                  deNewVDir.Properties[
" AccessRead " ][ 0 =   true
                  deNewVDir.Properties[
" AccessWrite " ][ 0 =   true
        
                  deNewVDir.CommitChanges();    
                  deRoot.CommitChanges();                    
                  deNewVDir.Close();    
                  deRoot.Close();    
              } 
              
catch  (Exception) 
              { 
                   
                  
throw
              } 
        } 
 
        
///   <summary>  
        
///  删除FTP虚拟目录 
        
///   </summary>  
        
///   <param name="virDirName"></param>  
         public   void  RemoveFTPVirDir( string  virDirName) 
        { 
            
string  strSchema  =   " IIsFtpVirtualDir "
            
string  strRootSubPath  =   " /MSFTPSVC/1/Root "
            
string  FtpName  =  virDirName; 
 
            
using  (DirectoryEntry root  =   new  DirectoryEntry( " IIS://localhost "   +  strRootSubPath)) 
            { 
                root.RefreshCache(); 
                
// 如果虚拟目录存在则删除 
                 foreach  (DirectoryEntry item  in  root.Children) 
                {                     
                    
if  (item.Name  ==  virDirName) 
                    { 
                        root.Invoke(
" Delete " new   string [] { strSchema, virDirName }); 
                        root.CommitChanges(); 
                        
break
                    } 
                } 
                root.Close(); 
            } 
        } 
 
        
public   string  GetBaseUrl(HttpContext context) 
        { 
            
return  
                context.Request.Url.Scheme 
+   " :// "   +  
                context.Request.Url.Authority 
+   
                context.Request.ApplicationPath; 
        } 
    } 
 

 
 
 
 

 

目录
相关文章
|
13天前
|
计算机视觉 Python
Flask学习笔记(六):基于Flask的摄像头-web显示代码(可直接使用)
这篇文章是关于如何使用Flask框架结合OpenCV库,通过电脑摄像头实现视频流在网页上的实时显示,并提供了单摄像头和多摄像头的实现方法。
42 2
Flask学习笔记(六):基于Flask的摄像头-web显示代码(可直接使用)
|
2月前
|
SQL 开发框架 安全
Web安全-IIS短文件名泄露
Web安全-IIS短文件名泄露
47 2
|
2月前
|
XML JSON 安全
Web安全-代码注入
Web安全-代码注入
20 6
|
3月前
|
中间件 API 开发者
Bottle框架探秘:如何用几行代码搅动Web开发江湖?
【8月更文挑战第31天】Bottle是一个仅依赖Python标准库的轻量级Web开发微框架,无需额外依赖,简化部署与维护。它以简洁高效著称,适合快速构建Web应用。通过简单的示例即可上手,如用几行代码实现“Hello World”应用。除基础功能外,Bottle还支持模板渲染、会话管理和表单处理等,适用于学习及小型项目,也能在高性能要求的应用中展现价值。无论是新手还是有经验的开发者,Bottle都是高效Web开发的理想选择。
38 1
|
3月前
|
开发框架 .NET 中间件
【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等
【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等
【Azure 云服务】在Cloud Service的代码中如何修改IIS Application Pool的配置呢? 比如IdleTimeout, startMode, Recycling.PeriodicRestart.Time等
|
3月前
|
前端开发 JavaScript
Web 前端大揭秘!JS 数据类型检测竟如此震撼,一场惊心动魄的代码探秘之旅等你来!
【8月更文挑战第23天】在Web前端开发中,合理检测数据类型至关重要。JavaScript作为动态类型语言,变量类型可在运行时变化,因此掌握检测技巧十分必要。
32 1
|
3月前
|
数据可视化 NoSQL Serverless
现代化 Web 应用构建问题之Serverless架构的Web站点费用计算如何解决
现代化 Web 应用构建问题之Serverless架构的Web站点费用计算如何解决
41 1
|
3月前
|
数据库 开发者 Java
数据战争:Hibernate的乐观与悲观锁之争,谁将主宰并发控制的王座?
【8月更文挑战第31天】在软件开发中,数据一致性至关重要,尤其是在多用户并发访问环境下。Hibernate 作为 Java 社区常用的 ORM 框架,提供了乐观锁和悲观锁机制来处理并发问题。乐观锁假设数据不易冲突,通过版本号字段 (`@Version`) 实现;悲观锁则假定数据易冲突,在读取时即加锁。选择哪种锁取决于具体场景:乐观锁适合读多写少的情况,减少锁开销;悲观锁适合写操作频繁的场景,避免数据冲突。正确应用这些机制可提升应用程序的健壮性和效率。
33 0
|
3月前
|
Java UED 自然语言处理
Struts 2 国际化竟有如此神奇魔力?快来揭开多语言支持的 Web 应用神秘面纱
【8月更文挑战第31天】在全球化背景下,Web应用需适应多种语言环境。Struts 2凭借其强大的国际化(i18n)支持,简化了多语言应用开发。通过不同语言的资源文件,它能自动匹配用户语言偏好,优化用户体验并扩展用户群。下面是一个示例:创建`messages.properties`(英语)与`messages_zh_CN.properties`(中文),并在Struts 2的Action类及JSP页面中调用`getText()`方法及Struts标签展示相应语言内容。此外,在struts.xml中指定资源文件,以确保框架正确加载对应语言包。通过这些步骤,开发者可以轻松实现应用的多语言支持。
61 0
|
3月前
|
Java 开发者 JavaScript
Struts 2 开发者的秘籍:隐藏的表单标签库功能,能否成为你下个项目的大杀器?
【8月更文挑战第31天】Struts 2表单标签库是提升Web页面交互体验的神器。它提供丰富的标签,如`&lt;s:textfield&gt;`和`&lt;s:select&gt;`,简化表单元素创建与管理,支持数据验证和动态选项展示。结合示例代码,如创建文本输入框并与Action类属性绑定,显著提升开发效率和用户体验。通过自定义按钮样式等功能,Struts 2表单标签库让开发者更专注于业务逻辑实现。
46 0