前端,比如jsp页面请求Controller中的action。示例代码如下:
//index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + path + "/";
String actionaddress=basePath+"house/list.action";
response.sendRedirect(actionaddress);
%>
或者也可以写成:
//index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
response.sendRedirect("house/list.action");
%>
而controller类如下:
@Controller
public class ProjectItemController {
private static final long serialVersionUID = 1L;
@Autowired
private ProjectItemService projectItemService;
@RequestMapping(value = "/house/list.action",method=RequestMethod.GET)
public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "4") Integer rows,Model model) {
Page<ProjectItem> projectItems = projectItemService.findProjectItemList(page, rows);
model.addAttribute("page", projectItems);
return "house";
}
}
注意controller类的映射路径开头一定有“/”,这一斜杠表示www.localhost:8080/工程名/(注意最后有一斜杠,不要落下),而index.jsp文件是在WebContent即工程部署好并发布后的工程根目录的下面,与WEB-INF文件夹在同一级目录,所以用重定向访问action,其路径为controller类中配置的路径的斜杠之后的内容!