Spring MVC - URL 处理问题

发布于 2024-09-27 00:49:23 字数 11456 浏览 5 评论 0原文

我正在尝试设置一个 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>

和我的控制器类:

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>

I'm trying to setup a Spring MVC project. I posted my configuration files below.
I'll be really glad if someone helps me.

Thanks in advance,
With regards,
Talha.

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="&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>

And my Controller class:

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗笺 2024-10-04 00:49:23

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

  1. 由于您使用的是 Context Config Loader Listener,因此 DispatcherServlet 的 contextConfigLocation 参数值应该为空。根据配置,您似乎正在为您的应用程序获取 2 个不同的应用程序上下文。
  2. 将 mvc-config.xml 文件包含在 applicationConfig.xml 文件中。
  3. webmvc servlet 的 url 映射应该是 /* (而不是 *.*)。请注意,这样做将导致您的资源请求(例如
  4. 您可能想缩减您的欢迎文件列表。
  5. 注释使您的对象不再是 POJO,但它们确实减少了您需要的 spring 配置量。

DispatcherServlet url 模式的初始化参数

<init-param>
    <param-name>contextConfigLocation<:/param-name> 
    <param-value><:/param-value> 
</init-param>

webmvc servlet 的

/*

Here are a few changes that I suggest.

  1. Since you are using the Context Config Loader Listener, the contextConfigLocation parameter value for the DispatcherServlet should be empty. As configured, it appears that you are getting 2 different application contexts for you application.
  2. Include the mvc-config.xml file in the applicationConfig.xml file.
  3. The url mapping for the webmvc servlet should be /* (not *.*). Note that doing this will cause your resource requests (ex. <img href="blah.gif>) to pass through spring. I dont have an elegant solution for this, but you can use the Spring ResourceServlet and put your resources in a jar.
  4. You probably want to trim down your welcome file list.
  5. Look into using annotations for your controller. While Annotations make your objects no longer POJOs, they do reduce the amount of spring configuration you need.

Init param for DispatcherServlet

<init-param>
    <param-name>contextConfigLocation<:/param-name> 
    <param-value><:/param-value> 
</init-param>

url pattern for the webmvc servlet

<url-pattern>/*</url-pattern>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文