【工程截图】
【所需jar包】
【web.xml配置】
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>action</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> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
【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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 注解开发方式 --> <mvc:annotation-driven /> <!-- 包自动扫描 --> <context:component-scan base-package="cn.Higgin.controller" /> <!-- 配置jsp视图解析器 内部资源视图解析器 前缀+逻辑名+后缀--> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
【myController.java】
package cn.Higgin.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyController { @RequestMapping("/goHome") // public String goHome(HttpServletRequest request){ System.out.println("33333333333333333333333333"); System.out.println(request.getRequestURI()); return "index"; //返回逻辑名 } @RequestMapping("/backHome") // public String backHome(HttpServletRequest request){ System.out.println("66666666666666666666666"); System.out.println(request.getRequestURI()); return "index"; //返回逻辑名 } }
【pages/index.jsp】(提示界面)
<body> This is my SpringMVC page. <br> </body>
【输出结果:输入 http://localhost:8080/SpringMVC_HiggiinCui/goHome.action】
控制台输出:
33333333333333333333333333
/SpringMVC_HiggiinCui/goHome.action
【输出结果:输入 http://localhost:8080/SpringMVC_HiggiinCui/backHome.action 】
控制台输出:
66666666666666666666666
/SpringMVC_HiggiinCui/backHome.action