请告诉我ServletActionContext和之间有什么区别ServletContext?
ServletContext在所有servlet和操作之间共享,而ServletActionContext特定于某个操作还是在所有操作之间共享?
在常规课程中,我需要阅读以下内容HttpContext:
控制器和操作名称
动作的属性(我可以通过HttpActionContext.ActionDescriptor.GetCustomAttributes<type>() 但这里我没有HttpActionContext- 我只有HttpContext)
阅读论证(例如actionContext.ActionArguments["paramName"],但同样 - 我只有一个HttpContext)
它不是动作过滤器,也不是控制器类。但是,我可以访问HttpContext.
httpcontext routedata asp.net-web-api actioncontext asp.net-web-api2
我正在为我的 .NET Core 应用程序设置一个自定义中间件来记录异常错误,并在 Startup.cs 中使用以下内容来注册我的上下文:
services.AddHttpContextAccessor();
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
Run Code Online (Sandbox Code Playgroud)
在我的配置中,我添加了以下内容以使用自定义中间件:
app.UseMiddleware<CustomMiddleware>();
Run Code Online (Sandbox Code Playgroud)
我的自定义中间件类如下:
public class CustomMiddleware
{
private readonly RequestDelegate next;
public CustomMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(HttpContext context)
{
try
{
this.BeginInvoke(context);
await this.next.Invoke(context);
this.EndInvoke(context);
}
catch(Exception ex)
{
//Capture the exception.
string hostName = Environment.MachineName;
string url = StringFunctions.getCurrentUrl(context);
string userName = string.Empty;
string controllerName = string.Empty;
string actionName = string.Empty;
//THIS IS NULL BELOW.
IActionContextAccessor contextAccessor = context.RequestServices.GetService(typeof(IActionContextAccessor)) as IActionContextAccessor;
RouteData routeData = …Run Code Online (Sandbox Code Playgroud) 我想创建一个登录页面,注销后我希望用户显示登录页面而不是上一页
注销后如何防止用户返回上一页.我已经清除了缓存....但是通过按下后退按钮用户将转到上一页.我想在退出后用户按下后按钮登录页面刷新并显示
<s:form action="Login" >
<s:textfield label="username" name="userName"/>
<s:password label="password" name="password"/>
<s:submit name="login" value="login"></s:submit>
</s:form>
Run Code Online (Sandbox Code Playgroud)
如何管理会话也.任何人帮我登录.java
package action;
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport {
private String userName;
private String password;
public Login() {
}
@Override
public String execute() {
Map session = ActionContext.getContext().getSession();
session.put("logged-in","yes");
return SUCCESS;
}
@Override
public void validate()
{
if(getUserName().length()==0)
{
addFieldError("userName", "User Name is required");
}
else if (!getUserName().equals("prerna"))
{
addFieldError("userName", "Invalid User");
}
if(getPassword().length()==0)
{
addFieldError("password", "password is required");
}
else if (!getPassword().equals("prerna")) …Run Code Online (Sandbox Code Playgroud) 在阅读了通过获取会话映射ActionContext.getContext().getSession()和通过注入它之间的区别之后,
SessionAware我想知道哪种方法是首选方法,为什么?
API 推荐使用SessionAware,我在网上读到使用SessionAware使应用程序更容易测试——测试是唯一的问题吗?
有人可以详细说明这个主题或指出解释这一点的参考资料吗?
假设我有一个带有定义值uploads.directory的struts.properties文件.如何以编程方式从Actioncontext访问该值?
要在 Struts 2 中获取 servlet 请求,我们可以使用ServletRequestAware 或ServletActionContext。但是,在特定的互联网资源中,被告知ServletRequestAware 应该使用 代替ServletActionContext。
这是否与ServletActionContext 将成为多线程环境中的共享资源这一事实有关,或者这背后是否有任何其他原因?
在我的应用程序中,我必须根据配置页面中选择的语言环境用户显示内容.我没有使用浏览器默认语言环境.
使用时s:text,它始终使用默认资源文件.
在Struts1中,我使用下面的代码在我的过滤器中设置默认语言环境
session.setAttribute("org.apache.struts.action.LOCALE",locale);
Run Code Online (Sandbox Code Playgroud)
如何在Struts2中动态设置用户选择的语言环境?
struts2 ×6
java ×3
servlets ×2
.net ×1
asp.net-core ×1
c# ×1
httpcontext ×1
locale ×1
middleware ×1
properties ×1
routedata ×1