首页 \ 问答 \ 无法弄清楚如何创建这个if语句(Cannot figure out how to create this if statement)

无法弄清楚如何创建这个if语句(Cannot figure out how to create this if statement)

这是世界上最简单的php如果声明曾经创建,我怎么也想不出怎么做,在一个。 基本上我有一个难倒的时刻,需要社区的帮助。

这是我的功能:

protected function _traverse_options($name, $type = ''){

    if(isset($this->_options[$name][$type])){
        echo $this->_options[$name][$type];
    }
}

我需要的if语句是检查三件事:

  • 如果type不为null 类型不是'之前'
  • 如果type不为null 类型不是'after'

我尝试过:

if($type != '' && $type != 'before' || $type != '' && $type != 'after'){}

这怎么都行不通。

我知道这很简单,但我想不出来? 应该||&& ??


This is the worlds easiest php if statement ever created, how ever ever I cannot figure out how to do it, in one. Essentially I am having a stumped moment and require the communities help .

This is my function:

protected function _traverse_options($name, $type = ''){

    if(isset($this->_options[$name][$type])){
        echo $this->_options[$name][$type];
    }
}

The if statement I need is to check for three things:

  • If type is not null but type is not 'before'
  • if type is not null but type is not 'after'

I tried doing:

if($type != '' && $type != 'before' || $type != '' && $type != 'after'){}

How ever that doesn't work.

I know this is simple, but I cannot figure it out? should || be && ??


原文:https://stackoverflow.com/questions/15669147
更新时间:2023-03-22 21:03

最满意答案

视图解析器的合约指定视图解析器可以返回null以指示无法找到视图。 但是,并非所有视图解析器都这样做,因为在某些情况下,解析器根本无法检测视图是否存在。 例如,InternalResourceViewResolver在内部使用RequestDispatcher,并且调度是确定JSP是否存在的唯一方法,但此操作只能执行一次。 VelocityViewResolver和其他一些同样适用。 检查特定视图解析程序的javadoc,以查看它是否报告不存在的视图。 因此,将一个InternalResourceViewResolver放在链中的最后一个位置会导致链未被完全检查,因为InternalResourceViewResolver将始终返回一个视图!

重定向到视图

如前所述,控制器通常返回逻辑视图名称,视图解析器将其解析为特定的视图技术。 对于通过Servlet或JSP引擎处理的JSP等视图技术,此解决方案通常通过InternalResourceViewResolver和InternalResourceView的组合来处理,InternalResolver和InternalResourceView通过Servlet API的RequestDispatcher.forward(..)方法或RequestDispatcher发出内部转发或包含。 .include()方法。 对于其他视图技术,例如Velocity,XSLT等,视图本身将内容直接写入响应流。

RedirectView的

作为控制器响应的结果,强制重定向的一种方法是控制器创建并返回Spring的RedirectView实例。 在这种情况下,DispatcherServlet不使用普通的视图解析机制。 而是因为它已经被赋予(重定向)视图,DispatcherServlet只是指示视图执行其工作。 RedirectView依次调用HttpServletResponse.sendRedirect()将HTTP重定向发送到客户端浏览器。

如果您使用RedirectView并且视图是由控制器本身创建的,则建议您将重定向URL配置为注入控制器,以便它不会被烘焙到控制器中,而是在上下文中与视图名称一起配置。 名为“重定向:前缀”的部分有助于此解耦。

重定向:前缀

虽然RedirectView的使用工作正常,但如果控制器本身创建RedirectView,则无法避免控制器意识到重定向正在发生的事实。 这实际上并不是最理想的,而且会把事情过于紧密。 控制器不应该真正关心如何处理响应。 通常,它应该仅根据已注入其中的视图名称进行操作。

特殊的重定向:前缀允许您完成此操作。 如果返回的视图名称具有前缀redirect:,则UrlBasedViewResolver(以及所有子类)会将此识别为需要重定向的特殊指示。 视图名称的其余部分将被视为重定向URL。

净效果与控制器返回RedirectView的效果相同,但现在控制器本身可以简单地按照逻辑视图名称进行操作。 逻辑视图名称(例如redirect:/ myapp / some / resource)将相对于当前Servlet上下文重定向,而重定向等名称: http//myhost.com/some/arbitrary/path将重定向到绝对URL。

转发:前缀

对于最终由UrlBasedViewResolver和子类解析的视图名称,也可以使用特殊的forward:前缀。 这会创建一个InternalResourceView(最终会对视图名称的其余部分执行RequestDispatcher.forward()),该视图名称被视为URL。 因此,此前缀对于InternalResourceViewResolver和InternalResourceView(例如,对于JSP)没有用。 但是,当您主要使用其他视图技术时,前缀可能会有所帮助,但仍希望强制Servlet / JSP引擎处理资源的转发。 (请注意,您也可以链接多个视图解析器。)

与redirect:前缀一样,如果将带有forward:前缀的视图名称注入控制器,则控制器不会检测到在处理响应方面发生了什么特殊情况。

DOCS: https//docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html


The contract of a view resolver specifies that a view resolver can return null to indicate the view could not be found. Not all view resolvers do this, however, because in some cases, the resolver simply cannot detect whether or not the view exists. For example, the InternalResourceViewResolver uses the RequestDispatcher internally, and dispatching is the only way to figure out if a JSP exists, but this action can only execute once. The same holds for the VelocityViewResolver and some others. Check the javadocs of the specific view resolver to see whether it reports non-existing views. Thus, putting an InternalResourceViewResolver in the chain in a place other than the last results in the chain not being fully inspected, because the InternalResourceViewResolver will always return a view!

Redirecting to Views

As mentioned previously, a controller typically returns a logical view name, which a view resolver resolves to a particular view technology. For view technologies such as JSPs that are processed through the Servlet or JSP engine, this resolution is usually handled through the combination of InternalResourceViewResolver and InternalResourceView, which issues an internal forward or include via the Servlet API’s RequestDispatcher.forward(..) method or RequestDispatcher.include() method. For other view technologies, such as Velocity, XSLT, and so on, the view itself writes the content directly to the response stream.

RedirectView

One way to force a redirect as the result of a controller response is for the controller to create and return an instance of Spring’s RedirectView. In this case, DispatcherServlet does not use the normal view resolution mechanism. Rather because it has been given the (redirect) view already, the DispatcherServlet simply instructs the view to do its work. The RedirectView in turn calls HttpServletResponse.sendRedirect() to send an HTTP redirect to the client browser.

If you use RedirectView and the view is created by the controller itself, it is recommended that you configure the redirect URL to be injected into the controller so that it is not baked into the controller but configured in the context along with the view names. The the section called “The redirect: prefix” facilitates this decoupling.

The redirect: prefix

While the use of RedirectView works fine, if the controller itself creates the RedirectView, there is no avoiding the fact that the controller is aware that a redirection is happening. This is really suboptimal and couples things too tightly. The controller should not really care about how the response gets handled. In general it should operate only in terms of view names that have been injected into it.

The special redirect: prefix allows you to accomplish this. If a view name is returned that has the prefix redirect:, the UrlBasedViewResolver (and all subclasses) will recognize this as a special indication that a redirect is needed. The rest of the view name will be treated as the redirect URL.

The net effect is the same as if the controller had returned a RedirectView, but now the controller itself can simply operate in terms of logical view names. A logical view name such as redirect:/myapp/some/resource will redirect relative to the current Servlet context, while a name such as redirect:http://myhost.com/some/arbitrary/path will redirect to an absolute URL.

The forward: prefix

It is also possible to use a special forward: prefix for view names that are ultimately resolved by UrlBasedViewResolver and subclasses. This creates an InternalResourceView (which ultimately does a RequestDispatcher.forward()) around the rest of the view name, which is considered a URL. Therefore, this prefix is not useful with InternalResourceViewResolver and InternalResourceView (for JSPs for example). But the prefix can be helpful when you are primarily using another view technology, but still want to force a forward of a resource to be handled by the Servlet/JSP engine. (Note that you may also chain multiple view resolvers, instead.)

As with the redirect: prefix, if the view name with the forward: prefix is injected into the controller, the controller does not detect that anything special is happening in terms of handling the response.

DOCS: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)