#千锋逆战# Spring MVC(1)

实现Controller接口方式

  1. 配置pom.xml

    • 
      
          4.0.0
      
          com.shj
          SpringMVC_test
          1.0-SNAPSHOT
      
          war
      
          
              
                  
                  
                      org.apache.maven.plugins
                      maven-compiler-plugin
                      3.6.1
                      
                          1.8
                          1.8
                      
                  
      
                  
                  
                      org.apache.tomcat.maven
                      tomcat7-maven-plugin
                      2.2
                      
                          /
                          8080
                      
                  
              
          
      
          
          
              
              
                  org.springframework
                  spring-webmvc
                  4.3.6.RELEASE
              
      
              
              
                  javax.servlet.jsp
                  jsp-api
                  2.2
                  provided
              
              
                  javax.servlet
                  javax.servlet-api
                  3.0.1
                  provided
              
      
             
              
                  jstl
                  jstl
                  1.2
              
          
      
      
      
  2. 配置web.xml

    • 
          dispatcherServlet
          org.springframework.web.servlet.DispatcherServlet
          
              contextConfigLocation
              classpath:spring-mvc.xml
          
      
      
      
          dispatcherServlet
          /
      
      

      配置dispatcherServlet前端控制器的servlet,引用class为org.springframework.web.servlet.DispatcherServlet

    • 填写init-param引用spring-mvc.xml的路径

  3. 配置spring-mvc.xml

    • 
      
      
          
          
              
              
          
      
          
          
      
          
          
          
      
      
      
  4. 实体类

    • public class Product {
          private String pname;
          private int pid;
          private double price;
      
          public String getPname() {
              return pname;
          }
      
          public void setPname(String pname) {
              this.pname = pname;
          }
      
          public int getPid() {
              return pid;
          }
      
          public void setPid(int pid) {
              this.pid = pid;
          }
      
          public double getPrice() {
              return price;
          }
      
          public void setPrice(double price) {
              this.price = price;
          }
      
          @Override
          public String toString() {
              final StringBuilder sb = new StringBuilder("Product{");
              sb.append("pname='").append(pname).append('\'');
              sb.append(", pid=").append(pid);
              sb.append(", price=").append(price);
              sb.append('}');
              return sb.toString();
          }
      }
      
  5. 实现ProductController实现Controller接口

    • public class ProductController implements Controller {
          @Override
          public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
              //返回一个虚拟页面
              return new ModelAndView("index");
          }
      }
      
  6. 实现index.jsp

    • 
          

      this is product input page

      <%--将结果上传至DetailProductController--%>
      pid:
      pname:
      price:
  7. 实现DetailProductController实现Controller接口

    • public class DetailProductController implements Controller {
          @Override
          public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
              int pid = Integer.parseInt(request.getParameter("pid"));
              String pname = request.getParameter("pname");
              double price = Double.parseDouble(request.getParameter("price"));
      
              Product product = new Product();
              product.setPid(pid);
              product.setPname(pname);
              product.setPrice(price);
             /*
              * detail:虚拟页面
              * 通过request域将值传递到页面
              * p:p为request域的name
              * product:request域的value
              * */
              return new ModelAndView("detail","p",product);
          }
      }
      
  8. detail.jsp页面

    • 
          

      this is detail input page

      <%--直接通过el表达式拿值--%> ${p}

通过注解的方式

  1. 配置web.xml,与上一种方式一样

    • 
          
              dispatcherServlet
              org.springframework.web.servlet.DispatcherServlet
              
                  contextConfigLocation
                  classpath:spring-mvc.xml
              
          
      
          
              dispatcherServlet
              /
          
      
      
      
  2. 配置spring-mvc.xml

    • 
      
      
          
          
              
              
          
      
          
          
      
          
          
      
          
          
      
      
      
      
  3. ProductController类

    • /*标识为Controller,内部实现Component注解,可以被扫描到*/
      @Controller
      public class ProductController{
          @RequestMapping("/up")
          public String UpProduct() {
              return "index";
          }
      }
      
  4. DetailProductController类

    • @Controller
      public class DetailProductController{
      
          @RequestMapping("/show")
          public String show(HttpServletRequest request, Model model) {
              int pid = Integer.parseInt(request.getParameter("pid"));
              String pname = request.getParameter("pname");
              double price = Double.parseDouble(request.getParameter("price"));
      
              Product product = new Product();
              product.setPid(pid);
              product.setPname(pname);
              product.setPrice(price);
      
              model.addAttribute("p", product);
      
              return "detail";
          }
      }
      

你可能感兴趣的:(#千锋逆战# Spring MVC(1))