一、什么是SpringMVC
Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据、业务与展现进行分离。从这样一个角度来说,Spring MVC和Structs、Structs2非常类似。Spring MVC的设计是围绕DispatcherServlet展开的,DispatcherServlet负责将请求派发到特定的handler。通过可配置的hander mappings、view resolution、来处理请求并且转到对应的视图。
二、Spring Web MVC处理请求的流程
web.XML配置
<!-- 前端控制器 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定位置 文件命名 必须是-servlet.xml结尾 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcherServlet-servlet.xml</param-value> <!-- 获取指定位置的xml -->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern><!-- 所有http请求都交给前端控制器处理 -->
</servlet-mapping>
1. 手动配置
Java代码实现 Controller 接口
public class UserController implements Controller {
private UserServer userServer = new UserServerImpl();
@Override
public ModelAndView UserController(HttpServletRequest request, HttpServletResponse response) throws Exception {
//调用业务层代码
User user= userServer.getUser();
ModelAndView mv = new ModelAndView();
mv.addObject("user", user);
mv.setViewName("user");
return mv;
}
}
xml 配置文件
<!-- HandlerMapping 处理器映射 -->
<!-- BeanNameUrlHandlerMapping:表示将请求的URL和Bean名字映射,如URL为 “http://localhost:8080/java1704-springmvc-ex01/user”,
则Spring配置文件必须有一个名字为“/user”的Bean,上下文默认忽略。 -->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- HandlerAdapter -->
<!-- SimpleControllerHandlerAdapter:表示所有实现了org.springframework.web.servlet.mvc.Controller接口
的Bean可以作为Spring Web MVC中的处理器。如果需要其他类型的处理器可以通过实现HandlerAdapter来解决。 -->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- ViewResolver 视图解析器 -->
<!-- InternalResourceViewResolver:用于支持Servlet、JSP视图解析;
viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,classpath中必须包含jstl的相关jar包;
prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比如传进来的逻辑视图名为hello,
则该该sp视图页面应该存放在“WEB-INF/jsp/hello.jsp”; -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 处理器 -->
<bean name="/user" class="com.hst.controller.UserController" /> <!-- 根据 user 访问 http://localhost:8080/SpringMvc/user-->
2.注解实现:
java代码:
@Controller
@RequestMapping("/user")
public class UserAonController {
@RequestMapping("/show")
public ModelAndView showUser(){
System.out.println(">>>>>>>>>>>>>>>");
User user=new User("root","123");
ModelAndView mv=new ModelAndView();
mv.addObject("userlist", user);
mv.setViewName("user");
return mv;
}
@RequestMapping("/add")
public ModelAndView addUser(){
System.out.println("<<<<<<<<<<<<<<<<<<<");
User user=new User("admin","123");
ModelAndView mv=new ModelAndView(); //获取ModelAndView
mv.addObject("userlist", user); //设置属性 ,jsp页面中获取
mv.setViewName("user"); //跳转的页面 ,必须和jsp文件名相同
return mv; //返回视图解析器
}
}
扫描注解:
<context:component-scan base-package="com.java1704.springmvc.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
3.获取参数
四种传参方式:
@RequestMapping("/add")
public String addUser(){
return "user/adduser";
}
//1.用注解赋值给变量
@RequestMapping("/add_submit")
public String addUserSubmit(@RequestParam("userName") String userName,@RequestParam("passWord") String passWord ) {
System.out.println("userName :"+userName);
System.out.println("passWord :"+passWord);
return "user/adduser";
}
//2.model属性名与form表单name值 一值 即可赋值
@RequestMapping("/add_submit_one")
public String addUserSubmitOne(String userName,String passWord,Model model) {
System.out.println("username :"+userName);
System.out.println("password :"+passWord);
model.addAttribute("message", userName+" "+passWord);
return "user/adduser";
}
//model属性名与form表单name值 一值 直接传入对象
@RequestMapping("/add_submit_two")
public String addUserSubmitTwo(User user,Map<String,String> modelMap) {
System.out.println("username :"+user.getUserName());
System.out.println("password :"+user.getPassWord());
modelMap.put("one", "张三"); // request.setAttrubut("one","张三");
modelMap.put("two", "123"); // request.setAttrubut("two","123");
return "user/adduser";
}
//request 获取表单值
@RequestMapping("/add_submit_three")
public String addUserSubmitThree(HttpServletRequest request,HttpServletResponse response) {
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");
System.out.println("username :"+userName);
System.out.println("password :"+passWord);
request.setAttribute("message", userName+" "+passWord);
return "user/adduser";
}