为了方便大神们的在线阅读,我把截图贴出来,再为了方便大神们帮我测试,我把代码也贴在截图下面,谢谢!
好了,开始贴截图+代码:
1,控制器
@Controller
@RequestMapping("/Hello")
public class HelloWorldController {
@RequestMapping("/a")
public ModelAndView test1(){
System.out.println("测试一下");
ModelAndView andView = new ModelAndView();
andView.addObject("msg", "供视图引用的一段信息");
andView.setViewName("/WEB-INF/views/Hello.jsp");
return andView;
2,配置文件
<?xml version="1.0" encoding="UTF-8"?>
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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">
<context:component-scan base-package="com.cailikun" />
<!-- 开启对spring注解的支持 ,为了低版本兼容(Spring3.2及以上已自带) -->
<context:annotation-config />
<!-- 静态资源访问 -->
<mvc:default-servlet-handler />
<!-- SpringMVC开启注解支持,也是为了低版本兼容 -->
<mvc:annotation-driven />
<!-- 配置上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<value>1048576</value>
</property>
</bean>
3,web.xml,有点长就不截图了,直接拷代码,抱歉
<?xml version="1.0" encoding="UTF-8"?>
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">
SpringMVC
<!-- 放置到最前面:处理中文乱码的过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!-- 告诉spring 按照什么编码进行转码 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 告诉spring配置文件的放置从哪里加载 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 配置了这个后,当服务器启动时,就开始实例化SpringMVC的核心控制器DispatcherServlet(有且只有一次) -->
<!-- 也就是说:服务器启动变慢,访问变快 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- url-pattern常用的两种配置方式: -->
<!--1, *.do 拦截以do为后缀的请求 -->
<!--2, / -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>