J2EE社区

苟有恒,何必三更起五更眠;
最无益,只怕一日曝十日寒.
posts - 241, comments - 318, trackbacks - 0, articles - 16
web.xml
    

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <!-- Standard Action Servlet Configuration -->
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>
   org.apache.struts.action.ActionServlet
  </servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
 </servlet>


 <!-- Standard Action Servlet Mapping -->
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
</web-app>




struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

 <form-beans>
  <form-bean name="loginForm" type="com.test.user.web.forms.LoginActionForm"/>
 </form-beans>
  
    <global-forwards>
  <forward name="success" path="/success.jsp"/>
    </global-forwards>
    
    <action-mappings>
     <action path="/loginPage" forward="/login.jsp"></action>
     <action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="loginForm" scope="request" />
    </action-mappings>
    <message-resources parameter="MessageResources" />
 
</struts-config>




applicationContext-beans.xml 

<?xml version="1.0" encoding="UTF-8"?>

<!--
  - Application context definition for JPetStore's business layer.
  - Contains bean references to the transaction manager and to the DAOs in
  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
  -->
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
 <bean id="userManager" class="com.test.user.web.manager.UserManagerImpl" />

</beans>



applicationContext-actions.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--
  - Application context definition for JPetStore's business layer.
  - Contains bean references to the transaction manager and to the DAOs in
  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
  -->
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 
  <bean name="/login" class="com.test.user.web.actions.LoginAction">
  <property name="userManager">
   <ref bean="userManager"/>
  </property>
 </bean>

</beans>



ActionFrom

package com.test.user.web.forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LoginActionForm extends ActionForm
{

 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private String username;
 private String password;
 public String getUsername()
 {
  return username;
 }
 public void setUsername(String username)
 {
  this.username = username;
 }
 public String getPassword()
 {
  return password;
 }
 public void setPassword(String password)
 {
  this.password = password;
 }
 @Override
 public void reset(ActionMapping mapping, HttpServletRequest request)
 {
  this.username = null;
  this.password = null;
 }
 @Override
 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request)
 {
  ActionErrors messages = new ActionErrors();
  if(this.username == null || this.password == null)
   messages.add("username", new ActionMessage("用户名或者密码不能为空"));
  return messages;
 }
 
 
}



Action:

package com.test.user.web.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.test.user.web.forms.LoginActionForm;
import com.test.user.web.manager.UserManager;

public class LoginAction extends Action
{
 private UserManager userManager;
 public void setUserManager(UserManager userManager)
 {
  this.userManager = userManager;
 }
 @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception
 {
  LoginActionForm loginForm = (LoginActionForm)form;
  
  String username = loginForm.getUsername();
  String password = loginForm.getPassword();
  
  userManager.login(username, password);
  
  return mapping.findForward("success");
 }
 
}



manager:

package com.test.user.web.manager;


public class UserManagerImpl implements UserManager
{

 @Override
 public void login(String username, String password)
 {
  System.out.println("在这可以再设置一个dao,然后结合hibernate操作数据库");
 }

}


 



运行结果报错:
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext
org.springframework.web.struts.DelegatingActionUtils.findRequiredWebApplicationContext(java:148)
org.springframework.web.struts.DelegatingActionProxy.getWebApplicationContext(java:139)
org.springframework.web.struts.DelegatingActionProxy.getDelegateAction(DelegatingActionProxy.java:120)
org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:105)
org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)



名称: ♪4C.ESL | .↗Evon
口号: 遇到新问题♪先要寻找一个方案乄而不是创造一个方案こ
mail: 联系我


Feedback

# re: javax.servlet.ServletException: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered错误  回复  更多评论   

2008-08-09 20:57 by xcp
怎么没得高手回呢,小弟在线等你们```````

# re: javax.servlet.ServletException: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered错误[未登录]  回复  更多评论   

2008-08-09 21:23 by chinajj
web.xml 里面加入
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

# re: javax.servlet.ServletException: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered错误  回复  更多评论   

2008-08-09 22:38 by BeanSoft
或者在这里:MyEclipse 5.5 开发 Spring + Struts + Hibernate 的详解视频http://www.blogjava.net/beansoft/archive/2007/10/07/150877.html

Spring 整合 Struts
• 添加 Spring Plug in
Ø <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
Ø <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
Ø </plug-in>
• 替换 Action 的 Type
Ø <action path="/login“ …
Ø type="org.springframework.web.struts.DelegatingActionProxy" />
• 在 Spring 配置文件中配置 Bean
Ø 要点: 通过 path 和 bean 的 name 进行匹配, 这两个值必须一样
Ø <bean name="/login" class="com.test.struts.action.LoginAction"></bean>

# re: javax.servlet.ServletException: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered错误  回复  更多评论   

2008-08-09 22:48 by zbh
看 刘长炯的视频!!

# re: javax.servlet.ServletException: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered错误  回复  更多评论   

2008-08-10 10:04 by xcp
好,谢谢各位了,问题解决了,方法如下:

方法一:
第一:发现原来是他的配置文件不是放在WEB-INF下,而是放在src目录下,解决的方法可将其spring的配置文件applicationContext转移阵地,转到WEB-INF下,或在web.xml下加入如下语句:
<!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔此参数用于后面的Spring-Context loader -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext*.xml,classpath*:applicationContext*.xml</param-value>

</context-param>

第二:在web.xml里面加上楼上说的
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>



方法二:就是楼上BeanSoft所说的加上一个plugin就可以了,但是值得注意的是上面的contextConfigLocation属性名是不可变的,而这个路径主要是看项目中spring配置文件放置地点..


在这谢谢各位哈!!

# re: javax.servlet.ServletException: java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered错误  回复  更多评论   

2008-12-18 15:59 by snidt
配置插件

只有注册用户登录后才能发表评论。


网站导航: