SpringMVC的学习____4.前端,控制器参数名不一致以及对象传递的解决方法

代码如下:

1.SpringMVC的web.xml文件:(DispatcherServlet配置)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


</web-app>

2.SpringMVC的配置文件 (springmvc-servlet.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!--包的自动扫描-->
    <context:component-scan base-package="com.xbf.controller"/>

    <!--静态资源过滤  SpringMVC不处理静态资源-->
    <mvc:default-servlet-handler/>

    <!--注解驱动-->
    <mvc:annotation-driven/>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

3.Controller层的编写:

package com.xbf.controller;


import com.xbf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {

    @RequestMapping("/t1/{name}")
    //restful风格
    //前端请求参数名和处理的参数名一致时
    //可以直接拿过来用
    //String name就是前端请求的参数名
    public String test1(@PathVariable String name, Model model){
        model.addAttribute("aaa",name);
        return "user";
    }

    @RequestMapping("/t2")
    //前端传递过来的参数名和控制器进行处理的参数名不一致时
    //使用 @RequestPara 注解进行解释说明
    // username:前端传递过来的参数名
    // name:控制器进行处理的参数名
    public String test2(@RequestParam(value = "username") String name,Model model){
        model.addAttribute("aaa",name);
        return "user";
    }


    @RequestMapping("/t3")
    //研究对象的传递
    //使用set方法给属性设置值,若没有传值就为默认值,设值前判断属性名是否一致呢???
    //会将前端传递过来的对象属性属性值对自动封装成对象 (user)
    public String test3(User user,Model model){
        System.out.println(user);
        model.addAttribute("user",user);
        return "user";
    }

    @RequestMapping("/t333/{id}/{name}/{age}")
    //restful风格
    public String test33(User user,Model model){
        System.out.println(user);
        model.addAttribute("user",user);
        return "user";
    }

    @RequestMapping("/t33/{id}/{name}/{age}")
    //restful风格
    public String test33(@PathVariable int id,@PathVariable String name,@PathVariable int age,Model model){
        User user = new User();
        user.setName(name);
        user.setId(id);
        user.setAge(age);
        System.out.println(user);
        model.addAttribute("user",user);
        return "user";
    }


}

4.前端页面 (user.jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

${aaa}


<hr>

<p>
    姓名:${user.getName()}
    ID:${user.getId()}
    年龄:${user.getAge()}
</p>

</body>
</html>

pojo实体类:

package com.xbf.pojo;

public class User {

    private int id;
    private String name;
    private int age;

    public User() {
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

总结:  DispatcherServlet将拦截的前端请求分发给某个具体的控制器,由控制器进行模型数据处理并返回。

猜你喜欢

转载自www.cnblogs.com/xbfchder/p/11324520.html
今日推荐