【问题标题】:Spring MVC - URL Handling ProblemSpring MVC - URL 处理问题
【发布时间】:2010-10-10 18:32:36
【问题描述】:

我正在尝试设置一个 Spring MVC 项目。我在下面发布了我的配置文件。 如果有人帮助我,我会非常高兴。

提前致谢, 带着敬意, 塔尔哈。

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
     <display-name>local</display-name>

     <context-param>
      <param-name>webAppRootKey</param-name>
      <param-value>webapp.root</param-value>
     </context-param>
     <context-param>
      <param-name>log4jConfigLocation</param-name>
      <param-value>/WEB-INF/log4j.xml</param-value>
     </context-param>
     <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
          /WEB-INF/applicationContext.xml</param-value>
     </context-param>

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

     <servlet>
      <servlet-name>webmvc</servlet-name>
      <servlet-class>
       org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>
        /WEB-INF/webmvc-servlet.xml
       </param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
     </servlet>

     <servlet-mapping>
      <servlet-name>webmvc</servlet-name>
      <url-pattern>*.*</url-pattern>
     </servlet-mapping>

     <session-config> 
      <session-timeout>30</session-timeout> 
     </session-config> 

     <filter>
      <filter-name>encoding-filter</filter-name>
      <filter-class>
                org.springframework.web.filter.CharacterEncodingFilter
      </filter-class>
      <init-param>
       <param-name>encoding</param-name>
       <param-value>UTF-8</param-value>
      </init-param>
      <init-param> 
       <param-name>forceEncoding</param-name> 
       <param-value>true</param-value> 
      </init-param> 
     </filter>

     <filter-mapping>
      <filter-name>encoding-filter</filter-name>
      <url-pattern>/*</url-pattern>
     </filter-mapping>

     <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>jsp/index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>jsp/default.jsp</welcome-file>
      <welcome-file>login.html</welcome-file>
      <welcome-file>login.htm</welcome-file>
      <welcome-file>jsp/login.jsp</welcome-file>
     </welcome-file-list>

     <error-page>
      <error-code>404</error-code>
      <location>/404.jsp</location>
     </error-page>
     <error-page>
      <exception-type>java.lang.Exception</exception-type>
      <location>/error.jsp</location>
     </error-page>

    </web-app>

webmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <bean id="localControllerMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver">
  <property name="prefix" value="hnd_"/>
 </bean>

 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"> 
   <value>org.springframework.web.servlet.view.JstlView</value>
  </property> 
  <property name="prefix"> 
   <value>/WEB-INF/jsp/</value>
  </property>
  <property name="suffix"> 
   <value>".jsp"</value>
  </property>
 </bean>

 <bean id="localController" class="view.LocalVIEW">
  <property name="methodNameResolver">
   <ref local="localControllerMethodNameResolver"/>
  </property>
  <property name="localBUS">
   <ref bean="localBUS"/>
  </property>
 </bean>

 <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
   <props>
    <prop key="/*.htm">localController</prop>
    <prop key="/*.html">localController</prop>
    <prop key="/*.jsp">localController</prop>
    <prop key="/*.ajax">localController</prop>
   </props>
  </property>
 </bean>

 <bean id="localBUS" class="bus.LocalBUS">
 </bean>

</beans>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<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"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

 <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref local="transactionManager" />
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="list*">PROPAGATION_REQUIRED, readOnly</prop>
    <prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
    <prop key="*"> PROPAGATION_REQUIRED, -MVARollbackException,+MVANoRollbackException </prop>
   </props>
  </property>
 </bean>


 <bean id="contextApplicationContextProvider" class="context.ApplicationContextProvider"/>  
 <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />

 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="baseSessionFactory" />
  </property>
 </bean>
 <!-- BUSINESS TARGET PRIMARY BUSINESS OBJECT: Hibernate implementation -->

 <bean id="localBUS" parent="txProxyTemplate" >
  <property name="target">
   <ref local="targetLocalBUS" />
  </property>
 </bean>


 <bean id="targetLocalBUS" class="bus.LocalBUS">
  <property name="localDAO">
   <ref local="local1DAO" />
  </property>
 </bean>

 <bean id="hibernateConfiguration"
  factory-bean="&amp;baseSessionFactory"
  factory-method="getConfiguration" />



 <bean id="local1DAO" class="dao.LocalDAO" >
  <property name="sessionFactory">
   <ref local="baseSessionFactory" />
  </property>
 </bean>

 <bean id="baseSessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
    <prop key="hibernate.show_sql">false</prop>
   </props>
  </property>
 </bean>


 <bean id="propPlaceholderConfigurer" class="advice.PropResolver" init-method="init">
  <property name="locationsMap">
   <props>
    <prop key="prod">/WEB-INF/prod.properties</prop>
   </props>
  </property>
  <property name="ignoreUnresolvablePlaceholders" value="true"/>
 </bean>

 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/>
  <property name="jdbcUrl">  
   <value>jdbc:oracle:thin:@${database.url}</value>
  </property>
  <property name="properties">
   <props>
    <prop key="user">${database.user}</prop>
    <prop key="password">${database.password}</prop>
   </props>
  </property>
  <property name="maxPoolSize" value="50"/>
  <property name="initialPoolSize" value="2"/>
  <property name="minPoolSize" value="1"/>
  <property name="maxStatements" value="200"/>
  <property name="maxIdleTime" value="300"/>
  <property name="acquireIncrement"  value="10"/>
  <property name="unreturnedConnectionTimeout" value="90"/>
  <property name="maxConnectionAge" value="120"/>
 </bean>

</beans>

还有我的 Controller 类:

package view;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import bus.LocalBUS;

public class LocalVIEW extends MultiActionController {

 /** Logger for this class and subclasses */
 protected final Log logger = LogFactory.getLog(getClass());

 private static Log log = LogFactory.getLog(LocalVIEW.class);

 protected LocalBUS localBUS;

 public LocalBUS getLocalBUS() {
  return localBUS;
 }

 public void setLocalBUS(LocalBUS localBUS) {
  this.localBUS = localBUS;
 }

 @SuppressWarnings({ "rawtypes", "unchecked" })
 public ModelAndView hnd_firstPage(HttpServletRequest request,
   HttpServletResponse response) throws IOException {
  try {
   ConcurrentMap map = new ConcurrentHashMap();
   map.put("ad", "TK");
   logger.info("SpringappController - returning firstPage view...   TKTK");
   return new ModelAndView("firstPage", map);
  } catch (Exception e) {
   e.printStackTrace();
   return null;
  }
 }

}

firstPage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>My First MVC Page</title>
</head>
<body>
<p>This is my name : ${ad}</p>
</body>
</html>

【问题讨论】:

  • 您需要帮助解决什么问题?
  • 不要在没有解释的情况下向我们倾倒一堆代码。告诉我们问题是什么,以及您需要什么帮助。

标签: jakarta-ee spring-mvc dispatcher


【解决方案1】:

以下是我建议的一些更改。

  1. 由于您使用的是上下文配置加载器侦听器,因此 DispatcherServlet 的 contextConfigLocation 参数值应该为空。根据配置,您的应用程序似乎获得了 2 个不同的应用程序上下文。
  2. 在 applicationConfig.xml 文件中包含 mvc-config.xml 文件。
  3. webmvc servlet 的 url 映射应该是 /*(不是 *.*)。请注意,这样做会导致您的资源请求(例如 ) 通过 spring。我对此没有优雅的解决方案,但您可以使用 Spring ResourceServlet 并将资源放入 jar .
  4. 您可能希望缩减欢迎文件列表。
  5. 考虑为您的控制器使用注释。虽然注释使您的对象不再是 POJO,但它们确实减少了您需要的弹簧配置量。

DispatcherServlet 的初始化参数

contextConfigLocation&lt:/param-name> &lt:/参数值>

webmvc servlet 的 url 模式

/*

【讨论】:

    猜你喜欢
    • 2011-05-02
    • 2011-02-01
    • 2012-09-14
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    相关资源
    最近更新 更多