第九章-springmvc框架

飞一样的编程
飞一样的编程
擅长邻域:Java,MySQL,Linux,nginx,springboot,mongodb,微信小程序,vue

分类: ssm 专栏: ssm框架课 标签: springmvc

2022-12-20 19:28:35 949浏览

springmvc框架的学习

1.MVC模式

2.搭建 springMVC环境

2.1第一个springMVC程序

  • 引入jar

  • 编写springmvc配置文件
  <bean name="/hellomvc" class="com.jf3q.part9.controller.HelloController">

    </bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
  • 修改web.xml
 <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>

        <!--加载springMVC的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--表示当前servlet会在系统启动时被载入-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  • 编写controller
public class HelloController extends AbstractController {


    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        System.out.println("springMVC框架搭建成功");
        return new ModelAndView("hello");
    }
}

2.2使用注解开发springMVC框架

  • 修改springmvc配制文件
    <!--开启springmvc注解驱动-->
    <mvc:annotation-driven/>

    <!--注解扫描包-->
    <context:component-scan base-package="com.jf3q.part9.controller"/>
    
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
  • 编写controller
@Controller
public class HelloController  {
    @RequestMapping("hellomvc")
   public  String hello(){
       return "hello";
   }
}

2.3springMVC框架梳理

3.前后端数据交互

3.1@RequestMapping注解

接收get请求

@RequestMapping(value = "hellomvc" ,method = RequestMethod.GET)

接受post请求

@RequestMapping(value = "hellomvc" ,method = RequestMethod.POST)

3.2入参处理

被@RequestParam 修饰后的参数默认是必传的,不带@RequestParam 的时候是选传,@RequestParam(required = false) 可以设置选传参数

 Logger log=Logger.getLogger(HelloController.class);
    @RequestMapping(value = "hellomvc" ,method = RequestMethod.GET)
   public  String hello(@RequestParam String username){
        log.info("前端传递的参数"+username);
       return "hello";
   }

三种入参处理

 @RequestMapping("/addUser")
    @ResponseBody
    public String addUser(String username,int age){

        System.out.println("username:"+username+"age:"+age);
        return "获取成功";
    }

      @RequestMapping("/addUser2")
    @ResponseBody
    public String addUser(UserInfo userInfo){

        System.out.println("username:"+userInfo.getUsername()+"\n age:"+userInfo.getAge());
        return "获取成功";
    }

    @RequestMapping("/addUser3")
    @ResponseBody
    public String addUser(HttpServletRequest request){

        System.out.println("username:"+request.getParameter("username")+"\n age:"+request.getParameter("age"));
        return "获取成功";
    }

3.3出参处理

1.ModelAndView的方式

  @RequestMapping(value = "hellomvc" ,method = RequestMethod.GET)
   public  ModelAndView hello(@RequestParam(required = false) String username){
        log.info("前端传递的参数"+username);
        ModelAndView modelAndView = new ModelAndView("hello");
        modelAndView.addObject("realName",username);
        return modelAndView;

   }

2.Model的方式传递

 @RequestMapping(value = "hellomvc" ,method = RequestMethod.GET)
   public  String hello(String username, Model model){
        log.info("前端传递的参数"+username);
        model.addAttribute("realName",username);
        return "hello";
   }

备注:model.addAttribute("realName",username);可以不写键,如果不写的话,默认使用对象的类型作为key

3.Map的方式传递

    @RequestMapping(value = "hellomvc" ,method = RequestMethod.GET)
   public  String hello(String username, Map map){
        log.info("前端传递的参数"+username);
        map.put("realName",username);
        return "hello";
   }

补充

像这种control里面啥也没干直接跳转目标页面的,这种可以换种写法——配置springmvc(纯纯的页面跳转)

传数组

  @RequestMapping("/reg")
    public String reg(String[] strs){
        System.out.println(Arrays.toString(strs));
        return "success";
    }
<form action="reg" method="post">
        <input name="strs"><br>
        <input name="strs"><br>
        <input name="strs"><br>
          <input type="submit" value="提交">

      </form>

传list

@Data
public class UserVo {

    List<UserInfo> users;
}

     @RequestMapping("/addUser2")
    public String addUser(UserVo userVo, Model model){

        System.out.println("userVo:"+userVo);

          model.addAttribute("userVo" ,userVo);

        return "success";
    }
 <f  orm action="addUser2" method="post">
       用户名1: <input name="users[0].username"  ><br>
        年龄1:<input name="users[0].age"><br>
        用户名2: <input name="users[1].username"  ><br>
        年龄2:<input name="users[1].age"><br>
          <input type="submit" value="提交">

      </form>

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695