创建一个SpringMVC小项目

来源:互联网 发布:怎么加入淘宝全球购 编辑:程序博客网 时间:2024/05/17 05:13
导包

/springMVC/WebContent/WEB-INF/lib/commons-logging-1.1.3.jar
/springMVC/WebContent/WEB-INF/lib/spring-aop-4.3.7.RELEASE.jar
/springMVC/WebContent/WEB-INF/lib/spring-beans-4.3.7.RELEASE.jar
/springMVC/WebContent/WEB-INF/lib/spring-context-4.3.7.RELEASE.jar
/springMVC/WebContent/WEB-INF/lib/spring-core-4.3.7.RELEASE.jar
/springMVC/WebContent/WEB-INF/lib/spring-expression-4.3.7.RELEASE.jar
/springMVC/WebContent/WEB-INF/lib/spring-web-4.3.7.RELEASE.jar
/springMVC/WebContent/WEB-INF/lib/spring-webmvc-4.3.7.RELEASE.jar


2  配置web.xml文件

 <!-- web.xml配置核心springMVC的Servlet分发器  The front controller of this Spring Web application, responsible for handling all application requests --><servlet><servlet-name>springDispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping><servlet-name>springDispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>    <!--这里是区分method的过滤器  -->  <filter>  <filter-name>methodFilter</filter-name>  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>    </filter>    <filter-mapping>    <filter-name>methodFilter</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>

这里需要注意:

Servlet分发器的url-pattern是/,而不是/*。原因在于优先级 /* > .jsp > / ,所以如果url-pattern是/*,则任意请求都要进入Servlet分发器,而请求可能只是访问index.jsp页面,不需要经过Servlet分发器处理。

如果没有用参数来说明springMVC配置文件的路径,采用默认配置的话,则默认路径是  /WEB-INF/<servlet-name>-servlet.xml

例如在WEB-INF文件夹下面有个springDispatcherServlet-servlet.xml,这样也可以找到。


3  配合springMVC核心文件

<?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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><context:component-scan base-package="com.anseon.controller"/><!--配置视图解析器   --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"/><property name="suffix" value=".jsp"/></bean></beans>

4  在代码中配置Controller的RequestMapping,请求路径的映射

@Controller@RequestMapping("/springmvc")public class Hello {    private static final String SUCCESS = "success";    /**     * 最终的视图:     *     prefix+returnValue+suffix     *     /WEB-INF/views/success.jsp     */    @RequestMapping("/hello")    public String execute(){        System.out.println("execute");        return SUCCESS;    }}

访问http://localhost:8080/springmvc01/index.jsp

然后执行操作之后返回的是/WEB-INF/views文件夹下的success.jsp页面。





问题:跳转到success.jsp页面的时候是重定向还是转发?

原创粉丝点击