1)假定你的请求为http://localhost:8080/springapp/index.jsp
2)为安全考虑,在index.jsp中将请求redirect到WEB-INF目录下:
3)在web.xml中,对*.htm的请求都发到
org.springframework.web.servlet.DispatcherServlet进行处理:
springapp
org.springframework.web.servlet.DispatcherServlet
1
springapp
*.htm
4)org.springframework.web.servlet.DispatcherServlet会获取当前应用名称apringapp,
并自动到WEB-INF目录下寻找并解析springapp-servlet.xml文件.
5)在springapp-servlet.xml文件中,将/hello.htm转发到springappController
springappController
6)在springapp-servlet.xml文件中,配置springappController对应的类文件
7)实现你自己的控制器类web.SpringappController,举例如下:
package web;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import bus.Product;
import bus.ProductManager;
public class SpringappController implements Controller {
/** Logger for this class and subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private ProductManager prodMan;
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 myModel = new HashMap();
myModel.put("now", now);
myModel.put("products", getProductManager().getProducts());
return new ModelAndView("hello", "model", myModel);
}
public void setProductManager(ProductManager pm) {
prodMan = pm;
}
public ProductManager getProductManager() {
return prodMan;
}
}
注意,控制器中采用了IOC模式的type2方式.
8)你返回的ModelAndView有三个参数,第一个是下个view的名称,
第二个是Model Object的别名,第三个是Model Object对象,后两个参数
将在下个VIEW中使用.