2020.06-Study_update.2

week 6.8-6.14

-Study-update
-MonRestFul风格
-Tue重定向和转发,乱码问题,JSON
-WesJSON时间工具类
-Thubootstrap
-Fri配置mybatis日志,mybatis模糊查询
-Sat-
-SunStudy-update

6.8 Monday

/**
 * @author lzr
 * @date 2020 06 08 19:31
 * @description restful风格
 */
@Controller
public class HelloController {
//    @RequestMapping(value = "/hello",method = RequestMethod.POST)
    @GetMapping("/hello/{a}/{b}")
    public String hello(@PathVariable int a,@PathVariable int b, Model model){
        model.addAttribute("msg","hello!!!!"+a+b);
        return "hello";
    }
    //post
    @PostMapping("/hello/{a}/{b}")
    public String hello1(@PathVariable int a,@PathVariable int b, Model model){
        model.addAttribute("msg","hellopost!!!!"+a+b);
        return "hello";
    }
}

@PathVariable

6.9 Tuesday

重定向

“redirect:”

/**
 * @author lzr
 * @date 2020/6/9 08:34:18
 * @description 重定向
 */
@Controller
public class RedirectController {
    @RequestMapping("/redirect")
    public String redirect(){
        return "redirect:/index.jsp";
    }
}

转发
“forward”

/**
 * @author lzr
 * @date 2020/6/9 08:34:18
 * @description 转发
 */
@Controller
public class RedirectController {
    @RequestMapping("/forward")
    public String redirect(){
        return "forward:/index.jsp";
    }
}

在springmvc中,如果配置了视图解析器,转发就直接return视图名,如果要重定向,则“redirect:+路径”,例如“redirect:/index.jsp"

接收参数和数据回显

/**
 * @author lzr
 * @date 2020/6/9 09:33:00
 * @description  接收参数和数据回显
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @GetMapping("/t1")
    public String test1(@RequestParam("username") String name, Model model){//接收前端的 @RequestParam为必须
        //1.接受前端参数
        //2.将返回的结果传递给前端 model
        model.addAttribute("msg",name);
        return "test";
    }
    //前端接收的是一个对象 id username age

    /**
     * 1.接收的前端传递是一个参数,判断参数的名字,假设名字直接在方法上,可以直接使用
     * 2.假设传递的是一个对象User,匹配User对象中的字段名,如果名字一致则赋值,否则失败。
     * @param user
     * @return
     */
    @GetMapping("/t2")
    public String test2(User user){
        System.out.println(user);
        return "test";
    }
}
乱码

配置过滤器
/*过滤所有请求包括jsp

<filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>com.maaoooo.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
/**
 * @author lzr
 * @date 2020/6/9 10:39:36
 * @description
 */
public class EncodingFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        filterChain.doFilter(servletRequest, servletResponse);//必须加上
    }
    
    public void destroy() {
    }
}
/**
 * @author lzr
 * @date 2020/6/9 10:21:51
 * @description
 */
@Controller
@RequestMapping("/en")
public class EncodingController {
    @PostMapping("t1")
    public String test1(@RequestParam("username") String name, Model model) {
        System.out.println(name);
        model.addAttribute("msg", name);
        return "test";
    }
}

使用mvc的filter

<filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

JSON

@ResponseBody能阻止视图解析器,
也可以在类上用@RestController 这样子下面的方法全部会返回字符串。
1,导包
2,配置乱码处理

<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <constructor-arg value="UTF-8"/>
        </bean>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                    <property name="failOnEmptyBeans" value="false"/>
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

3.新建一个ObjectMapper对象,用writeValueAsString()转换成JSON

/**
 * @author lzr
 * @date 2020/6/9 15:42:28
 * @description
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/j1")
    @ResponseBody//加了这个不会走视图解析器,会直接返回一个字符串
    public String json1() throws JsonProcessingException {
        //jackson,objectMapper
        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        User user1 = new User("无敌",12,"男");

        String json = mapper.writeValueAsString(user1);
        return json;
    }
}

返回json格式时间

    @RequestMapping("/j3")
    public String json3() throws JsonProcessingException {
        Date date = new Date();
        ObjectMapper mapper = new ObjectMapper();
        //不使用时间戳格式
        mapper.configure(SerializationFeature.CLOSE_CLOSEABLE, false);
        //使用自定义日期的格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        //传入格式
        mapper.setDateFormat(simpleDateFormat);

        return mapper.writeValueAsString(simpleDateFormat.format(date));
    }
}

6.10 Wednesday

JSON格式 时间已设置

/**
 * @author lzr
 * @date 2020/6/9 17:34:56
 * @description  返回JSON格式 时间设置 
 */
public class JsonUtil {
    /**
     * 不传格式
     * @param object
     * @return
     */
    public static String getJson(Object object){
        return getJson(object,"yyyy-MM-dd HH:mm:ss");
    }

    /**
     *  传格式
     * @param object
     * @param dateFormat
     * @return
     */
    public static String getJson(Object object,String dateFormat){
        ObjectMapper mapper=new ObjectMapper();
        //不使用时间戳方式
        mapper.configure(SerializationFeature.CLOSE_CLOSEABLE, false);
        //自定义日期格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        //传入格式
        mapper.setDateFormat(simpleDateFormat);
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

}

fastJSON常用方法

/**
 * FastJSON常用方法
 * @return
 */
@RequestMapping("/j4")
public String json4(){
    //java对象转json字符串
    User user = new User("xiaoliu",12,"男");
    String str=JSON.toJSONString(user);
    System.out.println(str);
    //json字符串转java
    User user1=JSON.parseObject(str,User.class);
    System.out.println(user1);
    //java对象转JSON对象
    JSONObject jsonObject= ((JSONObject) JSON.toJSON(user));
    System.out.println(jsonObject);
    //JSON对象转java
    User user2=JSONObject.toJavaObject(jsonObject,User.class);
    System.out.println(user2);

    return null;
}

6.12 Friday

在mybatis配置加入settings
必须要在最上方

<settings>
    <setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

模糊查询

<!--    //根据名字查询书籍  Books queryBookByName(String name);-->
    <select id="queryBookByName" resultType="Books" parameterType="String">
        select * from ssmbuild.books where bookName like concat('%',#{bookName},'%')
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值