gpt4 book ai didi

wicket - 如何将 Spring Rest 与 Apache Wicket Web 应用程序集成

转载 作者:行者123 更新时间:2023-12-02 09:31:56 24 4
gpt4 key购买 nike

我的 Apache Wiket + Spring Web 应用程序可以正常工作。目前 Spring 使用 DI 框架和过滤移动访问。我们计划在我们的应用程序中使用 Spring Rest,您能否建议我如何在我们现有的 web xml 中执行此操作。
最初,现有的 Web session 将使用 Rest Api 来访问数据(使用 ajax 调用的 ui 页面),因此我希望 Rest Controller 能够访问现有的 Wicket http session (如果使用已登录)。例如: 来自现有 http session 的 rest 调用应该能够访问现有的 http session 。任何想法 ?谢谢并感谢您的宝贵时间。

我的想法是使用“org.springframework.web.servlet.DispatcherServlet” 标记,但我怀疑我想通过这种方式共享同一 session 吗?

我现有的web.xml(工作)

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:myapp-spring-config.xml
</param-value>
</context-param>
<filter>
<filter-name>myapp</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationFactoryClassName</param-name>
<param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
</init-param>
</filter>

<filter>
<filter-name>myappMobileRequestFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>myappMobileRequestFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>

最佳答案

帮我解决了!感谢 martin-g 将我引导至 WicketSessionFilter 。我在这里发布答案以供将来引用或建议(如果需要)..

我/我们在我的“web.xml”中对上面发布的 web.xml 进行了更改。

  <filter>
<filter-name>SessionFilter</filter-name>
<filter-class>org.apache.wicket.protocol.http.servlet.WicketSessionFilter</filter-class>
<init-param>
<param-name>filterName</param-name>
<param-value>myapp</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/rest/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

如您所见,我添加了一个 WicketSessionFilter 来过滤由 spring DispatcherServlet 消耗的 /rest/* 请求。

如果你想在 Spring rest controller 中访问 spring session,你所要做的就是下面例如:

@RestController
@RequestMapping("/service")
public class TestServiceController {


@RequestMapping(value = "/getValue/{name}", method = RequestMethod.GET)
@SuppressWarnings("unused")
public String getValue(@PathVariable String name,HttpServletRequest request, HttpServletResponse response) {

HttpSession session = request.getSession(false);
WebSession wicketSession = session.getAttribute("wicket:wicket.myapp:session");

//rest of the code here ........
return result;
}

Spring-rest 和 Wicket 从此过上了幸福的生活....

关于wicket - 如何将 Spring Rest 与 Apache Wicket Web 应用程序集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32136136/

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