配置SpringMVC文件

  1. 创建项目并搭建Spring框架环境。

        环境搭建方法和ssh中spring一致

      2.配置web.xml配置文件

在web.xml文件中加入以下配置

<!-- SpringMVC配置 -->

       <servlet>

              <servlet-name>springmvc</servlet-name>

              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

              <init-param>

                     <param-name>contextConfigLocation</param-name>

                     <param-value>WEB-INF/classes/springmvc-servlet.xml</param-value>

              </init-param>

              <load-on-startup>1</load-on-startup>

       </servlet>

       <servlet-mapping>

              <servlet-name>springmvc</servlet-name>

              <url-pattern>/</url-pattern>

       </servlet-mapping>

       <!-- 配置spring中文编码过滤器 -->

       <filter> 

        <filter-name>characterEncodingFilter</filter-name> 

        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 

        <init-param> 

            <param-name>encoding</param-name> 

            <param-value>UTF-8</param-value> 

        </init-param> 

        <init-param> 

            <param-name>forceEncoding</param-name> 

            <param-value>true</param-value> 

        </init-param> 

    </filter> 

    <filter-mapping> 

        <filter-name>characterEncodingFilter</filter-name> 

        <url-pattern>/*</url-pattern> 

</filter-mapping> 

<!-- Spring配置 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<context-param>

              <param-name>contextConfigLocation</param-name>

              <param-value>WEB-INF/classes/applicationContext.xml</param-value>

</context-param>

<!--  Spring忽略静态资源 -->

<servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.jpg</url-pattern>

    </servlet-mapping>

       <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.png</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.js</url-pattern>

    </servlet-mapping>

    <servlet-mapping>

        <servlet-name>default</servlet-name>

        <url-pattern>*.css</url-pattern>

    </servlet-mapping>

3.配置springmvc-servlet.xml配置文件

将默认的applicationContext.xml配置文件复制一份修改名称为springmvc-servlet.xml

<beans

       xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:p="http://www.springframework.org/schema/p"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="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

       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

       <!-- 开启注解驱动 -->

       <mvc:annotation-driven/>

       <!-- 扫描spring组件 -->

       <context:component-scan base-package="com"></context:component-scan> 

</beans>

4.使用springMVC

创建com.controller包(相当于struts2中的com.action)

创建后台类UsersController(相当于struts2中的UsersAction)

 

@Controller

public class UsersController {

@RequestMapping(value="访问地址")

public String login(String 页面获取的参数1,String 页面获取的参数2){

return "跳转的页面";

}

}

全部评论

相关推荐

不愿透露姓名的神秘牛友
02-12 18:14
RT,这周五就是情人节了,前女友给我发了消息,我该不该回?
Yoswell:原则上来说让她滚,但是本着工作很累下班想吃瓜的心态,我觉得你可以回一下
点赞 评论 收藏
分享
2024-12-27 10:21
已编辑
海南师范大学 媒介策划
到我怀里来:身高体重住址这些就别写了,留几个关键的就行,工作经历突出重点写详细点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务