实现Controller接口方式
-
配置pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.shj</groupId> <artifactId>SpringMVC_test</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <build> <plugins> <!-- define the project compile level --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- 添加tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/</path> <port>8080</port> </configuration> </plugin> </plugins> </build> <dependencies> <!--Spring MVC的相关依赖,会自动导入8个包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.6.RELEASE</version> </dependency> <!-- jsp --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!--jstl相关依赖 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> </project>
-
配置web.xml
-
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
配置dispatcherServlet前端控制器的servlet,引用class为org.springframework.web.servlet.DispatcherServlet
填写init-param引用spring-mvc.xml的路径
-
-
配置spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--内部资源解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> <!--配置默认的servlet处理器,使得静态页面也可以直接访问--> <mvc:default-servlet-handler/> <!-- bean 的id属性值不能包含特殊字符 name可以,所以路径需要使用name来标识一个控制器的路径 --> <bean name="/ProductInput" class="com.qfedu.controller.ProductController"/> <bean name="/DetailProductController" class="com.qfedu.controller.DetailProductController"/> </beans>
-
实体类
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(); } }
-
实现ProductController实现Controller接口
public class ProductController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { //返回一个虚拟页面 return new ModelAndView("index"); } }
-
实现index.jsp
<body> <h1>this is product input page</h1> <%--将结果上传至DetailProductController--%> <form method="post" action="/DetailProductController"> pid:<input type="text" name="pid"> <br> pname:<input type="text" name="pname"> <br> price:<input type="text" name="price"> <br> <input type="submit"> </form> </body>
-
实现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); } }
-
detail.jsp页面
<body> <h1>this is detail input page</h1> <%--直接通过el表达式拿值--%> ${p} </body>
通过注解的方式
-
配置web.xml,与上一种方式一样
<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>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
-
配置spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--内部资源解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"/> <property name="suffix" value=".jsp"/> </bean> <!--配置默认的servlet处理器,使得静态页面也可以直接访问--> <mvc:default-servlet-handler/> <!--包扫描--> <context:component-scan base-package="com.qfedu.controller"/> <!--配置注解驱动--> <mvc:annotation-driven/> </beans>
-
ProductController类
/*标识为Controller,内部实现Component注解,可以被扫描到*/ @Controller public class ProductController{ @RequestMapping("/up") public String UpProduct() { return "index"; } }
-
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"; } }