springMVC(二)

一、RequestMapping使用

1.创建springMVC.xml文件

2在webapp下创建day02文件夹

3配置web.xml

4创建RequestMapping注解的使用类

5在day02创建index.jsp

//1.创建springMVC.xml文件   
 <!--3.配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--逻辑视图前缀-->
<!--        <property name="prefix" value="/day01/"></property>-->
        <property name="prefix" value="/day02/"></property>

        <!--逻辑视图前缀 匹配模式  逻辑视图前缀 + 逻辑视图 + 后缀  =   物理视图  -->
        <property name="suffix" value=".jsp"></property>

    </bean>

 

//3配置web.xml
<servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--如果不配置:  实际开发,都会配置
        servlet-name-servlet.xml 或者springmvc-servlet.xml
        路径必需放在  WEB-INF目录下
    -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springMVC.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

 

//4创建RequestMapping注解的使用类
@RequestMapping("/user")
@Controller
public class UserAction {
    @RequestMapping("/add")
    public  String add(Model model){
        model.addAttribute("msg","addUser");
        System.out.println("addUser");
        return "index";
    }
    @RequestMapping("/delete")
    public  String delete(){
        return "index";
    }
    @RequestMapping("/update")
    public  String update(){
        return "index";
    }
    @RequestMapping("/findById")
    public  String findById(){
        return "index";
    }
    @RequestMapping("/queryAll")
    public  String queryAll(){
        return "index";
    }
}





@RequestMapping("/items")
@Controller
public class ItemsAction {

    @RequestMapping("/add")
    public  String add(Model model){
        model.addAttribute("msg","addItems");
        System.out.println("addItems");
        return "index";
    }
    @RequestMapping("/delete")
    public  String delete(){
        return "index";
    }
    @RequestMapping("/update")
    public  String update(){
        return "index";
    }
    @RequestMapping("/findById")
    public  String findById(){
        return "index";
    }
    @RequestMapping("/queryAll")
    public  String queryAll(){
        return "index";
    }
}

 

//5在day02创建index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${msg}
</body>
</html>

 

二、参数绑定

int类型、String类型参数、封装数组类型参数、封装数组类型参数使用request、对象、包装对象、封装数组、封装集合、封装map

 

1.创建springmvc.xml文件

2在WEB-INF下创建jsp文件夹

3创建action测试数据类

4创建addUser.jsp页面

5创建index.jsp页面

6创建po实体类

7配置web.xml文件

 

一、解决中文String类型乱码问题

1.在项目中的web.xml中添加过滤器

  <!--配置一个过滤器  解决乱码问题  注意:必须方法在第一位-->
  <filter>
    <filter-name>characterEncoding</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>characterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

二、解决日期yyy-mm-nn的方法

1.在springMVC.xml注解中添加注解

2在User中的实体类中添加注解

//1.在springMVC.xml注解中添加注解
    <mvc:annotation-driven></mvc:annotation-driven>
//2在User中的实体类中添加注解
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;

三、参数绑定

//1.创建springmvc.xml文件
  <!--1.扫描装配bean-->
    <context:component-scan base-package="com.javacto"></context:component-scan>

    <!--我们现在开发第一种应用程序,可以都不配置-->
    <!--1.配置处理器映射器-->
    <!--2.配置处理器适配器-->

    <!--3.配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--逻辑视图前缀-->
       <!-- <property name="prefix" value="/day01/"></property>-->

        <!--WEB-INF 下面的文件不能直接访问,必需通过后台跳转-->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!--逻辑视图前缀 匹配模式  逻辑视图前缀 + 逻辑视图 + 后缀  =   物理视图  -->
        <property name="suffix" value=".jsp"></property>

    </bean>

    <!--解决日期yyy-mm-nn会自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdapter两个Bean,
      并且提供了数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,
      @Valid支持读写XML的支持(JAXB)和读写JSON的支持(默认Jackson)等功能-->
    <mvc:annotation-driven></mvc:annotation-driven>

 

//3创建action测试数据类


@Controller
@RequestMapping("/user")
public class UserAction {
    //跳转到 addUser.jsp
    @RequestMapping("goAddUser")
    public String goAddUser(){
        //  springmvc.xml文件有配置  /WEB-INF/jsp/
        return "addUser";
    }
    @RequestMapping("/reciveInt")
    public  String reciveInt(Integer id){
        System.out.println("接收到的值为"+id);
        return "index";
    }

    //如果是中文会有乱码,后面会讲如何解决
    @RequestMapping("/reciveStr")
    public  String reciveStr(String userName){
        System.out.println("接收到的值为"+userName);
        return "index";
    }

    @RequestMapping("/reciveArr")
    public  String reciveArr(Integer ids[]){
        for(int i:ids){
            System.out.println(i);
        }
        return "index";
    }

   /* //如果需要获取session  或者session中存值
    public  String getSession(HttpServletRequest request){
        //这样就可以使用session了
        HttpSession session = request.getSession();
        return "index";
    }*/

    @RequestMapping("/reciveStrRequest")
    public  void reciveStrRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String str = request.getParameter("userName");
        System.out.println(str);
        //请问这里返回的是物理视图  还是  逻辑视图  ?
        request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request,response);
    }

    @RequestMapping("/reciveUser")
    public  String reciveUser(User user)  {
        System.out.println(user);

        //如果get提交方式,还是乱码 就这样解决
        String aaa="ddd";
        // String name = new String(aaa.getBytes("iso-8859-1"),"UTF-8");
        return "index";
    }

    @RequestMapping("/reciveUserVo")
    public  String reciveUserVo(UserVo userVo){
        System.out.println(userVo);
        return "index";
    }

    @RequestMapping("/reciveList")
    public  String reciveList(UserVo userVo){
        System.out.println(userVo);
        return "index";
    }

    @RequestMapping("/reciveMap")
    public  String reciveMap(UserVo userVo){
        System.out.println(userVo);
        return "index";
    }

}

 

//4创建addUser.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>封装int类型参数</h1>
<%--这里需要做表单验证,以前怎么写现在就怎么写--%>
<form action="${pageContext.request.contextPath}/user/reciveInt.do" method="post">
    ID:<input name="id">
    <input type="submit" value="封装int类型">
</form>

<h1>封装String类型参数</h1>
<form action="${pageContext.request.contextPath}/user/reciveStr.do" method="post">
    用户名:<input name="userName">
    <input type="submit" value="封装String类型参数">
</form>

<h1>封装数组类型参数</h1>
<form action="${pageContext.request.contextPath}/user/reciveArr.do" method="post">
    来源:
    <input type="checkbox" name="ids" value="1"> 朋友介绍
    <input type="checkbox" name="ids" value="2"> 网络
    <input type="checkbox" name="ids" value="3"> CSDN
    <input type="submit" value="封装数组">
</form>

<h1>封装String类型参数 使用 request</h1>
<form action="${pageContext.request.contextPath}/user/reciveStrRequest.do" method="post">
    用户名:<input name="userName">
    <input type="submit" value="封装String类型参数">
</form>

<h1>封装对象</h1>
<form action="${pageContext.request.contextPath}/user/reciveUser.do" method="get">
    id:<input name="id">
    username:<input name="username">
    <%--  日期暂时只能输入这种格式  2020/2/23 后面会讲 --%>
    birthday:<input name="birthday">
    <input type="submit" value="封装对象">
</form>

<h1>封装包装类型对象</h1>
<form action="${pageContext.request.contextPath}/user/reciveUserVo.do" method="post">
    id:<input name="user.id">
    username:<input name="user.username">
    birthday:<input name="user.birthday">
    cccc:<input name="cccc">
    <input type="submit" value="封装对象">
</form>

<h1>封装数组</h1>
<form action="${pageContext.request.contextPath}/user/reciveUserVo.do" method="post">
    id:<input name="user.id">
    username:<input name="user.username">
    birthday:<input name="user.birthday">
    cccc:<input name="cccc">
    <input type="submit" value="封装对象">
</form>

<h1>封装集合 </h1>
<form action="${pageContext.request.contextPath}/user/reciveList.do" method="post">
    id1:<input name="lists[0].id">
    username1:<input name="lists[0].username">
    birthday1:<input name="lists[0].birthday">
    <br>
    id1:<input name="lists[1].id">
    username1:<input name="lists[1].username">
    birthday1:<input name="lists[1].birthday">
    <input type="submit" value="封装集合">
</form>


<h1>封装map使用很少很少</h1>
<form action="${pageContext.request.contextPath}/user/reciveMap.do" method="post">
    <%--id就是key--%>
    id:<input name="maps['id']">
    username:<input name="maps['name']">
    <input type="submit" value="封装map">
</form>


</body>
</html>

 

 

//5创建index.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

成功了
</body>
</html>

 

//6创建po实体类
public class User {
    private  int id;
    private  String username;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;


    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}




public class UserVo {
    //UserVo 包括了  User
    private  User user;

    //我自己也有自己属性
    private  String cccc;

    private List<User> lists;

    private Map maps;

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public List<User> getLists() {
        return lists;
    }

    public void setLists(List<User> lists) {
        this.lists = lists;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getCccc() {
        return cccc;
    }

    public void setCccc(String cccc) {
        this.cccc = cccc;
    }
}

 

//7配置web.xml文件

  <!--配置一个过滤器  解决乱码问题  注意:必须方法在第一位-->
  <filter>
    <filter-name>characterEncoding</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>characterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--之前servlet如何配置现就怎么配置-->
  <servlet>
    <servlet-name>sqb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--如果不配置:  实际开发,都会配置
        servlet-name-servlet.xml 或者springmvc-servlet.xml
        路径必需放在  WEB-INF目录下
    -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>sqb</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值