package sy.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
import sy.model.User; import sy.service.UserServiceI;
//springMvc的应用
//加上@Controller表示这是一个控制器类,将它加入到spring-mvc的框架中了 //在spring-mvc.xml中<context:component-scan base-package="sy.controller" />就能扫描到 @Controller //访问地址的注解@RequestMapping //在web.xml中配置了<url-pattern>*.do</url-pattern>所以("/userController.do")也不要写 @RequestMapping("/userController") public class UserController { private UserServiceI userService; public UserServiceI getUserService() { return userService; } @Autowired public void setUserService(UserServiceI userService) { this.userService = userService; }
@RequestMapping("/showUser") public String showUser(Integer id, HttpServletRequest request) { //因为在spring-mvc.xml 31行配置了 p:suffix=".jsp" //所以返回是showUser.jsp 文件的后缀.jsp不要写 User u = userService.getUserById(id); request.setAttribute("user", u); return "showUser"; } } 浏览器访问效果:
|