我的建议是使用 Spring 3。它是一个很酷的框架,符合 GAE。要将它与 GAE 一起用于 url 映射,您必须:
1 下载 Spring jar 并将其复制到 WEB-INF/lib
2 在 web.xml 中定义 Dispatcher servlet
<servlet>
<servlet-name>spring-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
3 在 web.xml 中指定 Dispatcher servlet 的映射。在示例中,我定义了两个 url 映射:从 /admin 和 /service 开始的所有请求都将在 Spring 中处理
<servlet-mapping>
<servlet-name>myspring</servlet-name>
<url-pattern>/admin/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myspring</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
4 创建控制器
// This controller process /admin addreses:
@Controller
public class AdminController {
@RequestMapping(value="/admin", method = RequestMethod.GET)
public String getAdminPage(HttpServletRequest request) {
return "/pages/admin/admin.jsp";
}
}
// This controller process /service addreses. Method getServicePage implements some
// RESTfull idea. You put in address id /service/123 and it return a page for this
// service
@Controller
public class ServiceController {
@Autowired
private ServiceDao serviceDao;
@RequestMapping(value="/service", method = RequestMethod.GET)
public String getServicesListPage(HttpServletRequest request) {
return "/pages/servise/service-list.jsp";
}
@RequestMapping(value="/service/{serviceId}", method = RequestMethod.GET)
public String getServicePage(HttpServletRequest request,
@PathVariable(value = "serviceId") int serviceId) {
Service service = serviceDao.get(serviceId);
request.setAttribute("service", service);
return "/pages/servise/service.jsp";
}
}
5 创建 Spring 上下文文件并指定扫描路径以进入控制器的上下文
根据 Dispatcher servlet 名称命名它,并根据您的控制器设置扫描包。认为您可以在没有示例的情况下处理它。