我有两个html模板,它们执行相同的功能,但由两个不同的控制器处理:
主持人控制器处理的第一个html
th:action="@{/someurl}"
管理员控制器处理的第二个HTML
y1=y1+45;// extra line u need to add
CGFloat y1 = 40;
CGFloat x1 = 30.0;
CGRect rect1 = CGRectMake(x1, y1, 100, 30);
for (int i = 0; i < 4; i++)
{
label = [[UILabel alloc] initWithFrame:rect1];
label.tag = i+1;
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor blueColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
label.text = @"Hello";
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotTapped:)];
[label addGestureRecognizer:tap];
[self.view addSubview:label];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(150, y1, 50, 30)];
label2.tag = i+100;
label2.textAlignment = NSTextAlignmentCenter;
label2.backgroundColor = [UIColor redColor];
label2.textColor = [UIColor whiteColor];
label2.font = [UIFont fontWithName:@"Verdana-Bold" size:8];
label2.text = [NSString stringWithFormat:@"%@%ld",@"world",(long)label2.tag];
label2.hidden = YES;
[self.view addSubview:label2];
rect1.origin.y += 45;
y1=y1+45;
NSLog(@"tag values of label2 are %ld",label2.tag);
}
正如您所看到的,这些模板的区别仅在于-(void)gotTapped:(UITapGestureRecognizer*)sender {
NSLog(@"%ld",(((UITapGestureRecognizer *)sender).view.tag));
int num= 99 +(int)(((UITapGestureRecognizer *)sender).view.tag);
NSLog(@"%d",num);
UILabel *label3 = [self.view viewWithTag:num];
label3.hidden = NO;
switch(((UITapGestureRecognizer *)sender).view.tag)
{
case 1:
NSLog(@"Clicked on label 1");
break;
case 2:
NSLog(@"Clicked on label 2");
NSLog(@"%ld",100 +((UITapGestureRecognizer *)sender).view.tag) ;
break;
case 3:
NSLog(@"Clicked on label 3");
NSLog(@"%ld",100 +((UITapGestureRecognizer *)sender).view.tag) ;
break;
case 4:
NSLog(@"Clicked on label 4");
NSLog(@"%ld",100 +((UITapGestureRecognizer *)sender).view.tag) ;
break;
}
}
url:
PostgreSQLDialect
是否可以将来自不同控制器的动态网址使用相同的模板?
答案 0 :(得分:3)
有很多不同的方法可以做到这一点......最简单的方法是在控制器中使用相同的模板,并在每个控制器中传递一个包含正确动作的变量。
例如:
// Moderator controller
@RequestMapping(value = "/moderator")
public String moderator(Map<String, Object> model) {
model.put("action", "/moderator/new-user-form");
return "new-user-form";
}
// Admin controller
@RequestMapping(value = "/moderator")
public String moderator(Map<String, Object> model) {
model.put("action", "/admin/new-user-form");
return "new-user-form";
}
并在html中
<form th:action="@{${action}}">
如果这不合适,您可以将表单本身转换为片段,然后将该操作作为参数传递。像这样:
<!-- Fragment -->
<form th:fragment="userform (action)" th:action="@{${action}}">
.
.
.
</form>
<!-- Including the fragment -->
<div th:replace="fragments/userform :: userform(action='/admin/new-user-form')" />
<!-- or -->
<div th:replace="fragments/userform :: userform(action='/moderator/new-user-form')" />