嗨我是春天的新世界,所以我想用@Controller表示法创建我自己的控制器,但我想我做的事情很糟糕。任何人都可以帮助我???
这是我的dispatch-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);
}}
运行它的结果是404,当hello.jsp存在时
答案 0 :(得分:0)
您的控制器类位于springapp.web
个包中,而context:component-scan
中的base-package
设置为hibernateTest.web
。
Spring无法检测到您的控制器,因为您的配置告诉它仅在hibernateTest.web
包中搜索带注释的类。
更改context:component-scan
中的包名称应解决此问题:
<context:component-scan base-package="springapp.web" />