本文将分享从JBoss中的servlet访问Springbean的详细内容,并且还将对java访问servlet进行详尽解释,此外,我们还将为大家带来关于idea启动springboot项目报错:ja
本文将分享从JBoss中的servlet访问Spring bean的详细内容,并且还将对java访问servlet进行详尽解释,此外,我们还将为大家带来关于idea 启动 springboot 项目 报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader () L...、Java Servlets 注入spring bean、java – Spring-security – 无法访问ServletException、java – 如何从Servlet访问托管bean和会话bean的相关知识,希望对你有所帮助。
本文目录一览:- 从JBoss中的servlet访问Spring bean(java访问servlet)
- idea 启动 springboot 项目 报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader () L...
- Java Servlets 注入spring bean
- java – Spring-security – 无法访问ServletException
- java – 如何从Servlet访问托管bean和会话bean
从JBoss中的servlet访问Spring bean(java访问servlet)
我想在JBoss中编写一个简单的servlet,它将在Spring bean上调用方法。目的是允许用户通过点击URL来启动内部工作。
在servlet中获取对Spring bean的引用的最简单方法是什么?
JBoss Web服务允许您使用@Resource批注将WebServiceContext注入到服务类中。在普通servlet中,有什么可比的东西吗?
答案1
小编典典你的servlet可以使用WebApplicationContextUtils来获取应用程序上下文,但是你的servlet代码将直接依赖于Spring Framework。
另一个解决方案是配置应用程序上下文,以将Spring bean作为属性导出到servlet上下文:
<bean> <property name="attributes"> <map> <entry key="jobbie" value-ref="springifiedJobbie"/> </map> </property></bean>
你的servlet可以使用以下方法从servlet上下文中检索Bean:
SpringifiedJobbie jobbie = (SpringifiedJobbie) getServletContext().getAttribute("jobbie");
答案2
小编典典有更复杂的方法可以做到这一点。有SpringBeanAutowiringSupport
里面org.springframework.web.context.support
可以让你建立这样的事情:
public class MyServlet extends HttpServlet { @Autowired private MyService myService; public void init(ServletConfig config) { super.init(config); SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext()); }}
这将导致Spring查找与其ApplicationContext
绑定的对象ServletContext
(例如通过创建ContextLoaderListener
),并注入其中可用的Spring bean ApplicationContext
。
idea 启动 springboot 项目 报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader () L...
有一次启动 springboot 项目的时候,报了一个非常奇怪的错误,说是找不到 servletContext,springboot 不是自带 tomcat 的吗?
在网上找了好久,说是用以下方式解决。
解决方式:
将 tomcat 的 lib 包下的 servlet-api.jar 拷贝到 java 的 jre 下的 lib 里即可。(需要在 idea 中重新导入一下 jdk)
mac 电脑下寻找 java 安装目录方式:在终端下执行
/usr/libexec/java_home -V
但是查看同事的 java 的 jre 包下并没有这个 jar,但是人家都能启动起来。这个就不知到为什么了?
虽然自己解决了这个问题,却不知所以然。。。。
后来我发现我们的项目里用的是 undertow 服务器,难道是跟这个有关系吗?
2. 另一种解决方式:
今天将项目的服务器由 tomcat 换成 undertow, 发现项目在本地可以启动成功,但是发不到服务器就启动不成功,就报跟上面同样的错误,原来是我本地的 jre 环境有 sevlet-api,但是我不能也往服务器的 jre 也放同样的吧,毕竟这样不合理。后来就查找原因,原来是 undertow 的 jar 包中已经有了 sevlet-api 3.1 版本的,但是为何启动不成功呢,原来我的另一个 jar 包中也有 sevlet-api,而且还是 2.5 版本的,
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>compile</scope>
</dependency>
项目在启动的时候优先找到了2.5版本的,而2.5版本的ServletContext类里没有getClassLoader这个方法,所以才报的错,解决方式是将2.5版本的排除,服务器就启动成功了。
总结:看来maven项目的依赖还得多研究。
Java Servlets 注入spring bean
Java Servlets 注入spring bean
方法一:HttpRequestHandler
- package com.xx.controller;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import org.springframework.web.HttpRequestHandler;
- import com.googlecode.jsonrpc4j.JsonRpcServer;
- @Component("myServlet")
- public class MyServlet implements HttpRequestHandler {
- @Autowired
- private TestManagerImpl testManagerImpl;
- @Override
- public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String requestURI = request.getRequestURI();
- requestURI = requestURI.substring(requestURI.lastIndexOf("/") + 1);
- JsonRpcServer server = testManagerImpl.getServer(requestURI);
- server.handle(request, response);
- }
- }
此类被spring托管,我们用@Component("myServlet")注解方法,属性
private TestManagerImpl testManagerImpl;
- <servlet>
- <display-name>myServlet</display-name>
- <servlet-name>myServlet</servlet-name>
- <servlet-class>
- org.springframework.web.context.support.HttpRequestHandlerServlet
- </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>myServlet</servlet-name>
- <url-pattern>/myurl/*</url-pattern>
- </servlet-mapping>
注意:servlet-class的配置类是:
org.springframework.web.context.support.HttpRequestHandlerServlet
</servlet-class>
方法二:利用org.springframework.beans.factory.annotation.Configurable 注解与 SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this) 实现
继续阅读:http://blog.csdn.net/doctor_who2004/article/details/53996422
java – Spring-security – 无法访问ServletException
我喜欢使用spring-security模块,但是当我从spring项目中学习教程(http://docs.spring.io/spring-security/site/docs/3.2.x/guides/helloworld.html)时,编译器会返回:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] \springframework\security\samples\config\SecurityWebApplicationInitializer.java:[5,7] error: cannot access servletexception
[ERROR]\springframework\security\samples\config\SecurityConfig.java:[10,7] error: cannot access Filter
[INFO] 2 errors
你能指导我出错吗?
.sourceEncoding>UTF-8.sourceEncoding>
.sourceEncoding}ecurity3-version>2.1.1.RELEASEecurity3-version>
.sonatype.org/content/groups/public/ecurity -->
figecurityecurity-webecurity3ecurity3-version}spring-ormslf4j-apicope>runtimecope>
cope>testcope>
cope>testcope>
cope>testcope>
cope>testcope>
cope>testcope>
nesisnesiscope>testcope>
cope>testcope>
MysqLmysql-connector-javafiguration>
figuration>
figuration>
figuration>
figuration>
figuration>
figuration>
.sourceEncoding}figuration>
最佳答案
尝试添加servlet-api依赖项
cope>providedcope>
java – 如何从Servlet访问托管bean和会话bean
这是我的commandLink如何工作
<p:dataTable value="#{myBean.users}" var="item"> <p:column> <h:commandLink value="#{item.name}" action="#{myBean.setSelectedUser(item)}" /> </p:column> </p:dataTable>
然后在myBean.java中
public String setSelectedUser(User user){ this.selectedUser = user; return "Profile"; }
假设用户名是彼得.然后,如果我点击彼得,我将把selectedUser设置为彼得的用户对象,然后重定向到配置文件页面,该配置文件页面现在从selectedUser呈现信息.我只想使用< h:outputText> ;,创建相同的效果,所以GET请求被记住.所以我这样做
<h:outputText value="{myBean.text(item.name,item.id)}" />
那么文本(String name,Long id)方法只是返回
"<a href=\"someURL?userId=\"" + id + ">" + name + "</a>"
所有剩下的都是创建一个servlet,捕获该id,查询数据库以获取用户对象,设置为selectedUser,重定向.
所以这里是我的servlet
public class myServlet extends HttpServlet { protected void doGet(HttpServletRequest request,HttpServletResponse response) throws servletexception,IOException { Long userId = Long.parseLong(request.getParameter("userId")); } }
现在我有id,如何访问我的会话bean来查询用户的数据库,然后访问managed bean将用户设置为selectedUser,然后重定向到profile.jsf?
解决方法
MyBean myBean = (MyBean) request.getSession().getAttribute("myBean");
也就是说,我有一种感觉,你在解决方案方向错误.你也可以这样做:
<a href="profile.jsf?userId=123">
在与profile.jsf关联的请求作用域bean中具有以下内容
@ManagedProperty(value="#{param.userId}") private Long userId; @ManagedProperty(value="#{sessionBean}") private SessionBean sessionBean; @postconstruct public void init() { sessionBean.setUser(em.find(User.class,userId)); // ... }
关于从JBoss中的servlet访问Spring bean和java访问servlet的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于idea 启动 springboot 项目 报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader () L...、Java Servlets 注入spring bean、java – Spring-security – 无法访问ServletException、java – 如何从Servlet访问托管bean和会话bean等相关知识的信息别忘了在本站进行查找喔。
本文标签: