这个问题可以有不同的答案,因为每个人都会按照自己的方式去做。但我想一个好的讨论可以帮助我找到我的。
首先,我将解释我尝试执行的场景。请耐心等待,这会很长(即使我试图保持简短)。因为我不是很有经验,所以请善待。
我需要控制用户访问我正在构建的Web应用程序的各种页面和功能。网页分为不同的模块。每个模块都有不同类型的访问选项。例如,模块1只能读取&写访问选项但模块2可以具有读,写,修改和删除访问选项。我根据需要构建了数据库,我只需要帮助构建前端来控制它。
我的想法中有以下内容(所有变量和列表都是在显示此页面之前调用的动作类中设置的):
<s:form id="form1" name="form1" action="savePermissions">
<s:select name="module" label="Select Module" list="moduleList" onchange="dojo.event.topic.publish('getPermissions');"/>
<table>
<s:iterator id="iter" value="emplList" var="emplVar">
<tr>
<td><s:textfield name="empl" value="%{<s:property value='emplName'/>}" readonly="true"/></td>
<td><s:select name="perm" value="%{<s:property value='permission'}" list="perList"/></td>
</tr>
</s:iterator>
</table>
<s:submit value="save"/>
</s:form>
此处从第1个下拉列表中选择模块时,会列出用户列表和相应的访问选项。现在,我应该能够为每个用户选择访问选项,然后使用这些值与操作类进行通信,以便我可以更新数据库。
但这里有两个问题:
1。)员工文本字段不会填充值。
2。)在动作类中,将在提交此表单时调用,如何确保获得与正确用户名相对应的正确值。我的意思是,如果&#39;读&#39;被选为&#39; user1&#39;并且&#39;查看&#39;对于&#39; user2&#39;在jsp页面上,我应该在动作类中得到相同的结构。
请告知。
谢谢!
如果不清楚,请发表评论。
修改
public class getPermissions1Action extends ActionSupport {
private List<String> moduleList;
public List<String> getModuleList() {
return moduleList;
}
public void setModuleList() {
this.moduleList = Arrays.asList("module1","module2","module3");
}
private empl emplVar;
private List<empl> emplList;
public List<empl> getEmplList() {
return emplList;
}
public void setEmplList(List<empl> emplList) {
this.emplList = emplList;
}
public empl getEmplVar() {
return emplVar;
}
public void setEmplVar(empl emplVar) {
this.emplVar = emplVar;
}
private List<String> perList;
public List<String> getPerList() {
return perList;
}
public void setPerList() {
this.perList = Arrays.asList("enter","view","edit","delete","nothing");
}
public getPermissions1Action() {
}
@Override
public String execute(){
empl var = new empl();
List<empl> empl = new ArrayList<empl>();
try{
for(int i=0;i<3;i++){
var.setEmplName("name");
var.setSelPer("view");
empl.add(var);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
setEmplList(empl);
setModuleList();
setPerList();
return SUCCESS;
}
}
empl class:
public class empl {
private String emplName;
private String selPer;
public String getEmplName() {
return emplName;
}
public void setEmplName(String emplName) {
this.emplName = emplName;
}
public String getSelPer() {
return selPer;
}
public void setSelPer(String selPer) {
this.selPer = selPer;
}
}
答案 0 :(得分:1)
The employee text fields do not get populated with the values.
你需要告诉/显示你的动作类中包含的iter
是什么?或者您是否期望在第一个select
组件中选择一些值,它应该填充迭代器的值,并且字段应该提供相应的迭代器值?
如果是这种情况,我担心它不会起作用,因为您需要一些ajax
功能,比如您需要刷新该区域并更新其内容,或者您需要从{解析该数据{1}}致电并需要填写受尊重的字段。
关于第二部分。如果您需要向用户提供多个权限,在这种情况下,虽然您可以启用多个选择选项,但我更喜欢每个用户前面的复选框列表,我可以选择根据我的选择/选择。
Personaly,为了提供对应用程序的基于角色的访问,我将选择其他方式,例如已经开发的Spring安全性,考虑到所有这类用例。
这是适合您的工作副本。
ajax
当迭代器迭代<s:form id="form1" name="form1" action="savePermissions">
<table>
<s:iterator value="emplList" var="emplVar">
<tr>
<td><s:textfield name="empl" value="%{emplName}" readonly="true"/>
</td>
</tr>
</s:iterator>
</table>
<s:submit value="save"/>
</s:form>
时,它会将对象放在您的案例emplList
中的值堆栈之上,我们可以直接访问其属性
仅供参考:根据命名惯例,类名应始终以大写字母开头
在Action类中有很多问题
empl
操作名称@Override
public String execute(){
empl var = new empl();
List<empl> empl = new ArrayList<empl>();
// Ther is no need to define this instead you can add directly to emplList
like emplList.add(object);
try{
for(int i=0;i<3;i++){
var.setEmplName("name");
var.setSelPer("view");
empl.add(var);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
setEmplList(empl);
setModuleList();
setPerList();
return SUCCESS;
}
应始终以大写字母(getPermissions1Action
)开头,但get / set应用于方法而非类名
我不确定为什么要定义Action Constructor
GetPermissions1Action