<bean:wirte>的用法

bean:write相当于<%=request.getAttribute("something")%> 
例子一: 


某处设置了request.setAttribute("hello","hello world"); 


则在某个jsp页面中,用struts的write标签取出并显示的方式如下: 


<bean:write name="hello"/>,则页面上显示出hello world。 




bean:write常用的属性有如下几个: 


1。name,用来指定属性的名字 


2。filter,用来指定是否屏蔽到属性值的HTML格式 


3。property,用来指定name所代表的对象的属性名字 


4。format,用来指定显示的时间,数字,日期等的格式 


例子二: 


某处设置了request.setAttribute("bj","<font color='red>欢迎你</font>"); 


则在某个jsp页面中,用struts的write标签取出并按红色的方式显示的方式如下: 


<bean:write name="bj" filter="false"/>,则页面上显示出红色的欢迎你。如果filter属性不设置为false,则默认为true,那么显示出的内容就为<font color='red>欢迎你</font>。 


例子三: 


某处设置了request.setAttribute("date",new Date()); 


则在某个jsp页面中,用struts的write标签取出并按指定方式显示日期的方法如下: 


<bean:write name=“date”/>,此为默认的显示方法,显示出的时间为:Fri Mar 28 15:04:21 CST 2008 


<bean:write name=“date” format="yyyy-MM-dd HH:mm:ss"/>,此为自己指定日期的显示格式,显示出的时间为2008-3-28 15:04:21 


例子四: 


某处设置了request.setAttribute("n",“1223333.333”); 


则在某个jsp页面中,用struts的write标签取出并按指定方式显示数字的方法如下: 


<bean:write name=“n”/>,此为默认的显示方法,显示出的数字位1223333.333 


<bean:write name=“n” format="###,###.####"/>,此为自己指定数字的显示格式,显示出的时间为1,223,333.333 


如果希望小数点后的不足四位时,缺位补0,则应 


<bean:write name=“n” format="###,###.0000"/>,此为自己指定数字的显示格式,显示出的时间为1,223,333.3330 


例子五: 


假如有User类和Groupe类,User类有属性名字userName,年龄age,性别sex和所属的Groupe,Groupe类有属性组名groupeName,并均具有相应的get和set方法。 


某处设置了request.setAttribute("user",new User("张三","20","男",new Groupe("三组"))); 


则在某个jsp页面中,用struts的write标签取出并按指定方式显示结构体的方法如下: 


用户名:<input type="text" value="<bean:write name=“user” property="userName"/>"> 


年龄:<input type="text" value="<bean:write name=“user” property="age"/>"> 


性别:<input type="text" value="<bean:write name=“user” property="sex"/>"> 


组名:<input type="text" value="<bean:write name=“user” property="groupe.groupeName"/>"> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个比较复杂的程序,需要考虑很多细节,包括文件读写、目录遍历等,需要仔细设计。下面我将给出一个简单的设计思路。 首先,我们需要定义一个目录结构体和一个文件结构体,用来表示文件系统的目录和文件。目录结构体应该包含目录名、子目录列表和文件列表,文件结构体应该包含文件名和文件内容。 ```C++ struct Directory { string name; vector<Directory> subdirs; vector<File> files; }; struct File { string name; string content; }; ``` 接下来,我们需要定义一个文件系统类,这个类应该包含一个根目录指针,以及一些方法来实现各种操作。其,改变目录、显示目录和创建目录操作都比较简单,只需要在目录结构体进行遍历即可。删除目录则需要注意递归删除子目录和文件。 ```C++ class FileSystem { public: FileSystem() { root = new Directory{"", {}, {}}; current_dir = root; } void change_dir(const string& dirname) { // 在当前目录查找子目录 for (auto& subdir : current_dir->subdirs) { if (subdir.name == dirname) { current_dir = &subdir; return; } } cout << "Directory not found!\n"; } void list_dir() { // 显示当前目录的子目录和文件 cout << "Directories:\n"; for (auto& subdir : current_dir->subdirs) { cout << subdir.name << "\n"; } cout << "Files:\n"; for (auto& file : current_dir->files) { cout << file.name << "\n"; } } void create_dir(const string& dirname) { // 创建子目录 current_dir->subdirs.push_back(Directory{dirname, {}, {}}); } void delete_dir(const string& dirname) { // 在当前目录查找子目录并删除 for (auto it = current_dir->subdirs.begin(); it != current_dir->subdirs.end(); ++it) { if (it->name == dirname) { current_dir->subdirs.erase(it); return; } } cout << "Directory not found!\n"; } private: Directory* root; Directory* current_dir; }; ``` 新建文件和打开文件操作也比较简单,只需要在当前目录查找文件即可。写入文件内容则需要注意将内容保存到文件结构体。 ```C++ class FileSystem { public: // ... void create_file(const string& filename) { // 创建文件 current_dir->files.push_back(File{filename, ""}); } void open_file(const string& filename) { // 在当前目录查找文件并打开 for (auto& file : current_dir->files) { if (file.name == filename) { current_file = &file; return; } } cout << "File not found!\n"; } void write_file(const string& content) { // 写入文件内容 if (current_file) { current_file->content += content; } } void delete_file(const string& filename) { // 在当前目录查找文件并删除 for (auto it = current_dir->files.begin(); it != current_dir->files.end(); ++it) { if (it->name == filename) { current_dir->files.erase(it); return; } } cout << "File not found!\n"; } private: // ... File* current_file = nullptr; }; ``` 最后,我们需要在主函数处理用户输入。这里我使用了一个简单的循环来获取用户输入并调用相应的方法。 ```C++ int main() { FileSystem fs; while (true) { string command, arg; cout << "> "; cin >> command; if (command == "cd") { cin >> arg; fs.change_dir(arg); } else if (command == "dir") { fs.list_dir(); } else if (command == "md") { cin >> arg; fs.create_dir(arg); } else if (command == "rd") { cin >> arg; fs.delete_dir(arg); } else if (command == "edit") { cin >> arg; fs.create_file(arg); fs.open_file(arg); } else if (command == "open") { cin >> arg; fs.open_file(arg); } else if (command == "write") { getline(cin, arg); fs.write_file(arg); } else if (command == "del") { cin >> arg; fs.delete_file(arg); } else if (command == "exit") { break; } else { cout << "Invalid command!\n"; } } return 0; } ``` 这个程序还有很多可以改进的地方,比如添加错误处理、支持文件重命名等。不过基本功能已经实现了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值