2015-05-25 105 views
0

我的應用程序出現映射錯誤,任何幫助將不勝感激! =)Spring - URI映射問題

錯誤消息:未發現與HTTP請求映射URI [/springapp/priceincrease.htm]在DispatcherServlet的名爲 'springapp'

,可以看到以下的一些代碼:

PriceIncreaseFormController.java

@Controller 
@RequestMapping(value="/priceincrease.htm") 
public class PriceIncreaseFormController { 

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

@Autowired 
private ProductManager productManager; 

@RequestMapping(method = RequestMethod.POST) 
public String onSubmit(@Valid PriceIncrease priceIncrease, BindingResult result) 
{ 
    if (result.hasErrors()) { 
     return "priceincrease"; 
    } 

    int increase = priceIncrease.getPercentage(); 
    logger.info("Increasing prices by " + increase + "%."); 

    productManager.increasePrice(increase); 

    return "redirect:/hello.htm"; 
} 

@RequestMapping(method = RequestMethod.GET) 
protected PriceIncrease formBackingObject(HttpServletRequest request) throws ServletException { 
    PriceIncrease priceIncrease = new PriceIncrease(); 
    priceIncrease.setPercentage(15); 
    return priceIncrease; 
} 

public void setProductManager(ProductManager productManager) { 
    this.productManager = productManager; 
} 

public ProductManager getProductManager() { 
    return productManager; 
} 

} 

應用-config.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 


    <bean id="productManager" class="com.companyname.springapp.service.SimpleProductManager"> 
    <property name="products"> 
     <list> 
      <ref bean="product1"/> 
      <ref bean="product2"/> 
      <ref bean="product3"/> 
     </list> 
    </property> 
    </bean> 


    <bean id="product1" class="com.companyname.springapp.domain.Product"> 
    <property name="description" value="Lamp"/> 
    <property name="price" value="5.75"/> 
    </bean> 

    <bean id="product2" class="com.companyname.springapp.domain.Product"> 
    <property name="description" value="Table"/> 
    <property name="price" value="75.25"/> 
    </bean> 

    <bean id="product3" class="com.companyname.springapp.domain.Product"> 
    <property name="description" value="Chair"/> 
    <property name="price" value="22.79"/> 
    </bean> 


    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="messages"/> 
    </bean> 

    <!-- Scans the classpath of this application for @Components to deploy as beans --> 
    <context:component-scan base-package="com.companyname.springapp.web" /> 

    <!-- Configures the @Controller programming model --> 
    <mvc:annotation-driven/> 

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

</beans> 

的web.xml

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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"> 

    <display-name>Springapp</display-name> 

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

    <servlet-mapping> 
    <servlet-name>springapp</servlet-name> 
    <url-pattern>*.htm</url-pattern> 
    </servlet-mapping> 

</web-app> 
+0

如何調用URL?該方法是POST,所以如果你不能從瀏覽器打開它。將方法更改爲GET(或GET和POST) – StanislavL

+0

感謝您的評論@StanislavL。我更改了控制器上的請求方法,但它沒有工作=( –

+0

您的控制器PriceIncreaseFormController存在哪個包? –

回答

0

你忘了加在你的應用程序上下文處理程序映射:formBackingObject:在你的GET方法

<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> 
+0

感謝您對@Akash的評論,但我添加了處理程序映射並且它仍然不工作=(我有另一個控制器,它工作正常,真誠我不知道是什麼導致問題 –

+0

,這樣'priceincrease.htm' url觸發? –

+0

我認爲「formBackingObject」,但我不是很確定,我是一個春季新手,並遵循教程=) –

0

有問題。 取決於您的要求,應該返回查看或使其成爲REST方法。

情況1:返回視圖(JSP,HTML,XSLT ...)

@RequestMapping(value="/priceincrease.htm",method = RequestMethod.GET) 
protected String formBackingObject(HttpServletRequest request,Model model) throws ServletException { 
     PriceIncrease priceIncrease = new PriceIncrease(); 
     priceIncrease.setPercentage(15); 
     model.addAttribute("priceIncrease",priceIncrease); 
     return "view_name"; 
} 

情況2:使用REST @ResponseBody方法(jackson.core罐是必要的)

@RequestMapping(value="/priceincrease.htm",method = RequestMethod.GET) 
@ResponseBody 
protected PriceIncrease formBackingObject(HttpServletRequest request) throws ServletException { 
    PriceIncrease priceIncrease = new PriceIncrease(); 
    priceIncrease.setPercentage(15); 
    return priceIncrease; 
} 

在這裏你有兩種情況下修改控制器的父映射(簡單地刪除它)

@Controller 
public class PriceIncreaseFormController { 

} 
+0

感謝您的評論@OomphFortuity。我試了兩種解決方案,你給了,(我複製它的字面意思,顯然改變「view_name」),但沒有作品=(我真的很困惑,不知道該怎麼試試更多... –

+0

請檢查,我已更新答案 –

+0

我曾嘗試過,並沒有工作,現在我嘗試了一些新的東西:在之前的評論中我說我有另一個控制器工作正常,所以我將它複製到這個控制器(假設它會工作),但它不是那麼奇怪 –