以下是项目文件以及 各个jsp文件中所写如的测试内容:
为了方便分析 有两个Controller类
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<servlet-name>mySpringmvc</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mySpringmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 设置运行tomcat时 默认打开的第一个jsp页面 即本例子中的WebRoot根目录下的index.jsp 页面-->
<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:p="http://www.springframework.org/schema/p"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 访问路径 类名,需要继承自AbstractController -->
<bean name="/index.html" class="controller.MyController"/>
<!-- 完成视图的对应 -->
<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/testJsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
对应Controller类 继承 AbstractController类
public class MyController extends AbstractController {
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse reponse) throws Exception {
System.out.println("进入我的控制器");
return new ModelAndView("index");
}
}
因为spring-servlet.xml中
<property name="prefix" value="/testJsp/"/>
<property name="suffix" value=".jsp"/>
运行非注解方式的结果为:
注解方式:
注解方式需要在 spring-servlet.xml中加入两行xml代码:
<context:component-scan base-package="controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
作用是扫描指定包下的类文件 进行注解
而是在其顶部采取注解方式:
@Controller
public class MyControllerZhujie {
@RequestMapping("/index.html")
public ModelAndView index() {
System.out.println("进入我的index");
return new ModelAndView("index");
}
@RequestMapping("/test.html")
public ModelAndView test() {
System.out.println("进入我的test");
return new ModelAndView("test");
}
}
而在spring-servlet.xml中的
<bean name="/index.html" class="controller.MyController"/>
即在注解模式下不需要再使用上述<bean>说明补充相关联路径字符串进入哪个包下的哪一个类
在相对应的实体类里用 @RequestMapping( ) 括号中定义相关联路径字符串即可
注解方式运行结果:
依次输入index.html 和 test.html后,控制台中分别输出 ControllerZhujie.java中定义的
方法中的输出语句