1

嗨,我是春季世界的新手,所以我想使用@Controller 表示法创建自己的控制器,但我想我做错了什么。任何人都可以帮助我???

这是我的调度-servlet

<bean  Class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>


 <context:component-scan base-package="hibernateTest.web" />

<context:annotation-config/>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>


<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

这是我的控制器

package springapp.web;

import com.sun.msv.reader.Controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.LogFactory;
import org.hibernate.validator.internal.util.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
import springapp.service.ProductManager;


@org.springframework.stereotype.Controller

public class HelloController {
protected final Log logger = (Log) LogFactory.getLog(getClass()); 
private ProductManager productManager; 


@RequestMapping("/hello.htm")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse  response)

        throws ServletException, IOException { 

    String now = (new java.util.Date()).toString();

    logger.info("returning hello view with " + now); 

    Map<String, Object> myModel = new HashMap<String, Object>();

    myModel.put("now", now);

    myModel.put("products", this.productManager.getProducts()); 

    return new ModelAndView("hello", "model", myModel);

 }}

当 hello.jsp 存在时,运行它的结果是 404

4

1 回答 1

0

您的控制器类在 package 中springapp.web,而在context:component-scan您已base-package设置为hibernateTest.web.

Spring 无法检测到您的控制器,因为您的配置告诉它仅在hibernateTest.web包中搜索带注释的类。

更改包名称context:component-scan应该可以解决这个问题:

<context:component-scan base-package="springapp.web" />
于 2013-11-09T23:49:58.757 回答