gpt4 book ai didi

javascript - Spring MVC 与 Mustache 发送字符串变量。

转载 作者:行者123 更新时间:2023-12-03 05:58:39 28 4
gpt4 key购买 nike

我正在尝试传递一个字符串值以在我的 js 脚本中使用,以阻止我应该渲染哪个 url。我已经尝试了一些方法,但我觉得与我将值从 java 传递到 js 的方式略有脱节。

这是我的 RequestMapping,我用它来将值从 java 发送到 js。

  @RequestMapping(value = HOME_URL_MAPPING)
public String inventory(final Model model) {

String userRole = "HQ";

model.addAttribute("userrole", userRole);
return controllerHelper.createUrl(INVENTORY, WebGlobals.HOME);
}

这是我用来构建 View 的 HTML 文件;

{{>partials/header}}

<script type="text/javascript">
//var gus = $("userrole").val(); // undefined
//var gus = '${userrole}'; //Returns ${userrole}
//var gus = $('userrole').val(); //This get undefined?
//var gus = $('userrole'); //This returns [object Object]
//var gus = ${'userrole'}; //Dont work
//var gus = ${userrole}; // Dont work
//var gus = ${userrole} Dont Work
//var gusRole = ${userrole.name} Dont work
//var gus = {{userrole}} //undeefined
//var gus = {{userrole}}; //undefined
var gus1 = "HQ"

var haveClicked = false;
var myRole = "ADMIN"


function checkUserRights()
{
alert("The Ships Going down all on account of the weather");
alert(gus); //Prints off the value of Gus which will be the role of the user

if(haveClicked == false)
{
switch(myRole) // I want to put gus here to test
{
case "ADMIN":
alert("Opening ADMIN Rights");
$('<a href="/inventory/view"> H Group</a>').appendTo($('body'));
$('<a href="/inventory/approve"> A Group</a>').appendTo($('body'));
haveClicked = true;
break;
case "HQ":
alert("Opening H Rights");
$('<a href="/inventory/view"> H Group</a>').appendTo($('body'));
haveClicked = true;
break;
default:
alert("Opening A Rights");
$('<a href="/inventory/approve"> A Group</a>').appendTo($('body'));
haveClicked = true;
}
}//end of if checker

}

 {{>partials/footer}}

这是我的控制助手。

@Component
public class ControllerHelper {
private static final Logger logger = LoggerFactory.getLogger(ControllerHelper.class);

private final MessageSource messages;
private final String version;

@Autowired
public ControllerHelper(
final MessageSource messages,
@Value("${version}") final String version)
{
this.messages = Validate.notNull(messages);
this.version = Validate.notBlank(version);
}

public String getVersion() {
return StringUtils.stripToEmpty(version);
}

public void addCommonAttributes(final Model model) {
if (model == null) return;
model.addAttribute("version", getVersion());
}

public String getMessage(final String code, final String defaultMessage) {
return messages.getMessage(code, null, defaultMessage, getLocale());
}

public String getMessage(final String code, final Object[] args, final String defaultMessage) {
return messages.getMessage(code, args, defaultMessage, getLocale());
}

public static Locale getLocale() {
return Globals.DEFAULT_LOCALE;
}

public static String getCurrentUserName() {
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth == null) return "anonymous";
return auth.getName();
}

public static ResponseEntity<Map<String, Object>> displayErrorResponse(final Exception exception) {
String errorMessage = exception.getMessage();

if (exception.getClass() == InvalidUserInputException.class) {
logger.info("USER ERROR: " + errorMessage);
}
else {
logger.error("ERROR: " + errorMessage);
}

final Map<String, Object> error = new HashMap<>();
error.put("error", errorMessage);

ResponseStatus exceptionResponseStatus = exception.getClass().getAnnotation(ResponseStatus.class);

// Set as bad request if no response status set for exception
return ResponseEntity.status(exceptionResponseStatus == null ? HttpStatus.INTERNAL_SERVER_ERROR : exceptionResponseStatus.value()).body(error);
}

public String createUrl(String... pathElements) {
int numberOfPathElements = pathElements.length;
if (numberOfPathElements == 0) return "";

String result = pathElements[0];
for (int iter = 1; iter < numberOfPathElements; iter++) {
result += "/" + pathElements[iter];
}

return result;
}

}

最佳答案

试试这个

var gus = '{{userrole}}';

关于javascript - Spring MVC 与 Mustache 发送字符串变量。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39826407/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com