GVKun编程网logo

从JBoss中的servlet访问Spring bean(java访问servlet)

14

本文将分享从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)

从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...

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

Java Servlets 注入spring bean


方法一:HttpRequestHandler

此方法要实现HttpServlet,我们不能直接实现HttpServlet接口,而去实现HttpRequestHandler接口:
[java]  view plain  copy
  1. package com.xx.controller;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.springframework.beans.factory.annotation.Autowired;  
  10. import org.springframework.stereotype.Component;  
  11. import org.springframework.web.HttpRequestHandler;  
  12.   
  13. import com.googlecode.jsonrpc4j.JsonRpcServer;  
  14.   
  15. @Component("myServlet")  
  16. public class MyServlet implements HttpRequestHandler {  
  17.   
  18.     @Autowired  
  19.     private TestManagerImpl testManagerImpl;  
  20.   
  21.     @Override  
  22.     public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  23.         String requestURI = request.getRequestURI();  
  24.         requestURI = requestURI.substring(requestURI.lastIndexOf("/") + 1);  
  25.         JsonRpcServer server = testManagerImpl.getServer(requestURI);  
  26.         server.handle(request, response);  
  27.   
  28.     }  
  29.   
  30. }  

此类被spring托管,我们用@Component("myServlet")注解方法,属性
    @Autowired
    private TestManagerImpl testManagerImpl;
   是我们需要注入的对象。

我们还需要在web.xml中配置此伪servlet:
[java]  view plain  copy
  1. <servlet>  
  2.     <display-name>myServlet</display-name>  
  3.     <servlet-name>myServlet</servlet-name>  
  4.     <servlet-class>  
  5.         org.springframework.web.context.support.HttpRequestHandlerServlet  
  6.     </servlet-class>  
  7. </servlet>  
  8.   
  9. <servlet-mapping>  
  10.     <servlet-name>myServlet</servlet-name>  
  11.     <url-pattern>/myurl/*</url-pattern>  
  12. </servlet-mapping>  

注意:servlet-class的配置类是:
      <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

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

java – 如何从Servlet访问托管bean和会话bean

参见英文答案 > Get JSF managed bean by name in any Servlet related class6
这是我的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&gt ;,创建相同的效果,所以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?

解决方法

JSF将会话作用域管理的bean存储为会话属性,使用受管Bean名称作为关键字.所以下面的工作(假设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 beanjava访问servlet的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于idea 启动 springboot 项目 报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader () L...、Java Servlets 注入spring bean、java – Spring-security – 无法访问ServletException、java – 如何从Servlet访问托管bean和会话bean等相关知识的信息别忘了在本站进行查找喔。

本文标签: