对于想了解Spring弃用的ExpressionEvaluationUtils评估方法的替代方法?的读者,本文将是一篇不可错过的文章,我们将详细介绍spring被淘汰了,并且为您提供关于327-Eva
对于想了解Spring弃用的ExpressionEvaluationUtils评估方法的替代方法?的读者,本文将是一篇不可错过的文章,我们将详细介绍spring被淘汰了,并且为您提供关于327 - Evaluating Simple C Expressions、6-5 Evaluate Postfix Expression (25 分)、@Autowired in Spring PermissionEvaluator、asp.net-mvc – ASP.NET MVC ValidateInput(false)停止使用xVal和[RegularExpression] DataAnnotation的有价值信息。
本文目录一览:- Spring弃用的ExpressionEvaluationUtils评估方法的替代方法?(spring被淘汰了)
- 327 - Evaluating Simple C Expressions
- 6-5 Evaluate Postfix Expression (25 分)
- @Autowired in Spring PermissionEvaluator
- asp.net-mvc – ASP.NET MVC ValidateInput(false)停止使用xVal和[RegularExpression] DataAnnotation
Spring弃用的ExpressionEvaluationUtils评估方法的替代方法?(spring被淘汰了)
自从 Spring
3.x发行版以来,不推荐使用org.springframework.web.util.ExpressionEvaluationUtils类,因此我正在Java代码中寻找EL表达式评估的另一种方法。
阅读JSP 2.x文档后,我确实设法构造了一种替代方法:
import javax.el.ELContext;import javax.el.ExpressionFactory;import javax.el.ValueExpression;import javax.servlet.jsp.JspApplicationContext;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspFactory;import javax.servlet.jsp.PageContext;public class ExpressionEvaluationUtils { public static Object evaluate(String exp, Class<?> resultClass, PageContext pageContext) throws JspException { if (pageContext == null){ return exp; } ELContext elContext = pageContext.getELContext(); JspFactory jf = JspFactory.getDefaultFactory(); JspApplicationContext jac = jf .getJspApplicationContext(pageContext.getServletContext()); ExpressionFactory ef = jac.getExpressionFactory(); ValueExpression val = ef.createValueExpression(elContext, exp, resultClass); return val.getValue(elContext); }}
您能为我的实施方案提出更多替代方案吗?
答案1
小编典典您可以使用以下任何项目:
- Apache Commons OGNL
- MVEL
- JBoss EL
- spring表达语言(SpEL)
327 - Evaluating Simple C Expressions
题意:
C 表达式运算, 变量为 a-z, 代表运算数为 1-26; 运算符包括 +, -, ++, --; 要求输出原表达式的运算结果, 及运算完后各个变量的值.
1. 每个变量只会出现一次;
2. 不会出现 a+++b 这样带歧义的表达式;
3. ++ 或 -- 不会既出现在变量前面, 又出现在后面.
思路:
1. 把空格去掉;
2. 把 ++ 与 -- 去掉, 把相应的变量按先缀/后缀计算完替换成数字;
(1). 从字符串起始开始, 每次往后数2位, 看这三位是否满足 a++, a--, ++a, --a 这样的形式, 若满足, 则这是一个前缀/后缀表达式的变量;
3. 在进行 2 的时候, 把碰到的每个变量的值都记录下来(用 map), 如果有前缀/后缀运算, 就把运算完后变量的结果记录下来;
4. 每碰到一个运算符, 接下来一定是一个变量, 此时, 只需根据运算符 +, - , 把当前变量与前面的计算结果进行运算即可.
要点:
map 的 key 默认就是字典序的, 所以输出时, 直接用 iterator 进行 ++ 输出即可.
题目:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=263
代码:
# include <iostream>
# include <string>
# include <cstdio>
# include <cstring>
# include <vector>
# include <algorithm>
# include <cctype>
# include <iterator>
# include <assert.h>
# include <map>
using namespace std;
typedef map<char, int>::iterator MIT;
// 本运算式中使用的变量
map<char, int> usedVariables;
map<char, int> variables;
// 初始化 a-z 为 1-26
void initVariables(map<char, int>& variables) {
for (int i=0; i<26; i++) {
variables[''a'' + i] = i + 1;
}
}
// 去除空格
string removeSpace(const string& line) {
string result;
for (int i=0; i<line.size(); i++) {
if (!isspace(line[i])) result += line[i];
}
return result;
}
// 判断从 index 往后 3 位是否为 ++a 格式
bool isPrefixAdd(const string& expression, int index) {
return expression[index] == ''+'' &&
expression[index+1] == ''+'' &&
islower(expression[index+2]);
}
// 判断从 index 往后 3 位是否为 --a 格式
bool isPrefixMinus(const string& expression, int index) {
return expression[index] == ''-'' &&
expression[index+1] == ''-'' &&
islower(expression[index+2]);
}
// 判断从 index 往后 3 位是否为 a++ 格式
bool isPostfixAdd(const string& expression, int index) {
return islower(expression[index]) &&
expression[index+1] == ''+'' &&
expression[index+2] == ''+'';
}
// 判断从 index 往后 3 位是否为 a-- 格式
bool isPostfixMinus(const string& expression, int index) {
return islower(expression[index]) &&
expression[index+1] == ''-'' &&
expression[index+2] == ''-'';
}
// 获取 expression[index] 处的变量值, 若是正常变量, 则index 前进 1
// 若取到一个 ++ 或 -- , 则 index 前进 3
// 并把变量的值存放到 usedVariables 中
// 返回值: first 为变量值, second 为下一步操作的 index
pair<int, int> getVariable(const string& expression, int index) {
int value;
char c;
if (isPrefixAdd(expression, index)) { // ++a
index = index+2;
c = expression[index];
value = variables[c] + 1;
usedVariables[c] = value;
} else if (isPrefixMinus(expression, index)) { // --a
index = index+2;
c = expression[index];
value = variables[c] - 1;
usedVariables[c] = value;
}else if (isPostfixAdd(expression, index)) { // a++
c = expression[index];
index = index+2;
value = variables[c];
usedVariables[c] = value + 1;
}else if (isPostfixMinus(expression, index)) { // a--
c = expression[index];
index = index+2;
value = variables[c];
usedVariables[c] = value - 1;
} else { // a
assert(islower(expression[index]));
c = expression[index];
value = variables[c];
usedVariables[c] = value;
}
return make_pair(value, ++index);
}
// 计算表达式
int calcExpression(const string& expression) {
int i = 0;
pair<int, int> variable;
// 取第一个变量
variable = getVariable(expression, i);
int value = variable.first;
i = variable.second;
// 如果到结尾了, 说明这里只有一个变量
if (i >= expression.size()) return value;
while (i < expression.size()) {
// 取运算符
char op = expression[i];
++i;
// 取接下来的变量, 有运算符就必定有变量
variable = getVariable(expression, i);
int next = variable.first;
i = variable.second;
// 计算
if (op == ''+'') {
value += next;
} else { // op == ''-''
value -= next;
}
}
return value;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("327_i.txt", "r", stdin);
freopen("uva_o.txt", "w", stdout);
#endif
initVariables(variables);
string line;
while (getline(cin, line)) {
usedVariables.clear();
string expression = removeSpace(line);
int value = calcExpression(expression);
// 输出
printf("Expression: %s\n", line.c_str());
printf(" value = %d\n", value);
for (MIT it = usedVariables.begin(); it != usedVariables.end(); it++) {
printf(" %c = %d\n", it->first, it->second);
}
}
return 0;
}
环境: C++ 4.5.3 - GNU C++ Compiler with options: -lm -lcrypt -O2 -pipe -DONLINE_JUDGE
6-5 Evaluate Postfix Expression (25 分)
Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /.
Format of functions:
ElementType EvalPostfix( char *expr );
where expr
points to a string that stores the postfix expression. It is guaranteed that there is exactly one space between any two operators or operands. The function EvalPostfix
is supposed to return the value of the expression. If it is not a legal postfix expression, EvalPostfix
must return a special value Infinity
which is defined by the judge program.
Sample program of judge:
#include <stdio.h>
#include <stdlib.h> typedef double ElementType; #define Infinity 1e8 #define Max_Expr 30 /* max size of expression */ ElementType EvalPostfix( char *expr ); int main() { ElementType v; char expr[Max_Expr]; gets(expr); v = EvalPostfix( expr ); if ( v < Infinity ) printf("%f\n", v); else printf("ERROR\n"); return 0; } /* Your function will be put here */
Sample Input 1:
11 -2 5.5 * + 23 7 / -
Sample Output 1:
-3.285714
Sample Input 2:
11 -2 5.5 * + 23 0 / -
Sample Output 2:
ERROR
Sample Input 3:
11 -2 5.5 * + 23 7 / - *
Sample Output 3:
ERROR
代码:
ElementType EvalPostfix( char *expr ) {
char t[10];
double s[100],flag;
int c = 0,k = 0;
while(expr[k]) {
int j = 0;
while(expr[k] && expr[k] != '' '') t[j ++] = expr[k ++];
t[j] = 0;
if(t[0] >= ''0'' && t[0] <= ''9'' || t[1] && (t[0] == ''-'' || t[0] == ''+'')) {
s[c] = 0;
int i = 0;
flag = 1;
if(t[i] == ''-'') {
flag = -1;
i ++;
}
if(t[i] == ''+'') i ++;
while(t[i] && t[i] != ''.'') {
s[c] = s[c] * 10 + t[i ++] - ''0'';
}
if(t[i] == ''.'') {
i ++;
double d = 0.1;
while(t[i]) {
s[c] += (t[i ++] - ''0'') * d;
d *= 0.1;
}
}
s[c] *= flag;
c ++;
}
else {
if(c < 2) return Infinity;
switch(t[0]) {
case ''+'':s[c - 2] += s[c - 1];break;
case ''-'':s[c - 2] -= s[c - 1];break;
case ''*'':s[c - 2] *= s[c - 1];break;
case ''/'': if(s[c - 1] == 0) return Infinity;
s[c - 2] /= s[c - 1];break;
}
c --;
}
while(expr[k] && expr[k] == '' '') k ++;
}
if(c > 1) return Infinity;
return s[0];
}
@Autowired in Spring PermissionEvaluator
首先,我已经广泛搜索了这个,虽然似乎有一个固定的地方我无法成功引用PermissionEvaluator中注入的@Bean:
https://jira.springsource.org/browse/SEC-2136?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
在该问题的评论部分,Rob Winch提供了一个解决方案的建议
to work around this issue,you can proxy your permissionEvaluator using LazyInitTargetSource
话虽这么说,我在实现发布的XML的基于注释的JavaConfig版本时遇到了麻烦.我正在使用Spring Boot 1.0.0.BUILD-SNAPSHOT和spring-boot-starter-security.
我有一个类来配置方法安全性,如下所示:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(new MyPermissionEvaluator());
expressionHandler.setParameterNamediscoverer(new SimpleParameterdiscoverer());
return expressionHandler;
}
}
并且PermissionEvaluator的开始:
public class MyPermissionEvaluator implements PermissionEvaluator {
private static final Logger LOG = LoggerFactory.getLogger(MyPermissionEvaluator.class);
@Autowired
private UserRepository userRepo;
@Override
public boolean hasPermission(Authentication authentication,Object targetDomainObject,Object permission) {
if (authentication == null || !authentication.isAuthenticated()) {
return false;
}
if (permission instanceof String) {
switch((String) permission) {
case "findUser":
return handleUserPermission(authentication,targetDomainObject);
default:
LOG.error("No permission handler found for permission: " + permission);
}
}
return false;
}
@Override
public boolean hasPermission(Authentication authentication,Serializable targetId,String targettype,Object permission) {
throw new RuntimeException("Id-based permission evaluation not currently supported.");
}
private boolean handleUserPermission(Authentication auth,Object targetDomainObject) {
if (targetDomainObject instanceof Long) {
boolean hasPermission = userRepo.canFind((Long) targetDomainObject);
return hasPermission;
}
return false;
}
}
需要做什么才能从PremissionEvaluator中获取对UserRepository的引用?我尝试了各种变通方法,但没有成功.似乎没有任何东西可以@Autowired到PermissionEvaluator ……
asp.net-mvc – ASP.NET MVC ValidateInput(false)停止使用xVal和[RegularExpression] DataAnnotation
[RegularExpression(“^ [^”] * $“,ErrorMessage =”特殊字符是不允许的。“)],我再次收到”潜在危险请求…“错误,尽管[ValidateInput(false) ]指令。
发生了什么?是否有更简单的正则表达式验证方法,但是使用[ValidateInput(false)]?当然,我想在我的验证码在模型,而不是在控制器。
解决方法
No,it was an issue in MVC 1 + xVal.
In MVC 2 the validation works as
supposed (and there’s no need for xVal
anymore) – Alex42
看起来,机器人一直把这个推到顶部。你可以将答案标示为接受,以便知道吗?
今天关于Spring弃用的ExpressionEvaluationUtils评估方法的替代方法?和spring被淘汰了的分享就到这里,希望大家有所收获,若想了解更多关于327 - Evaluating Simple C Expressions、6-5 Evaluate Postfix Expression (25 分)、@Autowired in Spring PermissionEvaluator、asp.net-mvc – ASP.NET MVC ValidateInput(false)停止使用xVal和[RegularExpression] DataAnnotation等相关知识,可以在本站进行查询。
本文标签: