SSM中SpringMvc层配置

SSM中SpringMvc层配置

web.xml

<!DOCTYPE web-app PUBLIC
       "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
       "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
        version="4.0">
 <display-name>Archetype Created Web Application</display-name>
 
!--  配置spring监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件,所以需要复制一份到WEB-INF,但不如都放在resources方便管理
也可以设置配置文件路径,让其在resources中查找配置文件-->
 <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
<!--  设置文件路径-->
 <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
 </context-param>

servlet>
   <servlet-name>dispatcherServlet</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>
 <servlet-mapping>
   <servlet-name>dispatcherServlet</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>
<!--  此处我还用到了谷歌的Kaptcha验证码功能,该功能具体配置也是在web.xml中实现的-->
<servlet>
   <servlet-name>Kaptcha</servlet-name>
   <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<!--    是否有边框-->
   <init-param>
     <param-name>kaptcha.border</param-name>
     <param-value>no</param-value>
   </init-param>
<!--    字体颜色-->
   <init-param>
     <param-name>kaptcha.textproducer.font.color</param-name>
     <param-value>red</param-value>
   </init-param>
<!--    字体大小-->
   <init-param>
     <param-name>kaptcha.textproducer.font.size</param-name>
     <param-value>43</param-value>
   </init-param>
<!--    字体-->
   <init-param>
     <param-name>kaptcha.textproducer.font.names</param-name>
     <param-value>Arial</param-value>
   </init-param>
<!--    干扰线,阴影-->
   <init-param>
     <param-name>kaptcha.noise.color</param-name>
     <param-value>black</param-value>
   </init-param>
<!--    图片宽度(之所以是图片就是防止复制验证码粘贴)-->
   <init-param>
     <param-name>kaptcha.image.width</param-name>
     <param-value>135</param-value>
   </init-param>
<!--    图片高度-->
   <init-param>
     <param-name>kaptcha.image.height</param-name>
     <param-value>50</param-value>
   </init-param>
<!--    使用那些字符生成验证码-->
   <init-param>
     <param-name>kaptcha.textproducer.char.string</param-name>
     <param-value>ABCDEFGHIJKLMNOPQRST123456789</param-value>
   </init-param>
<!--    验证码长度-->
   <init-param>
     <param-name>kaptcha.textproducer.char.length</param-name>
     <param-value>4</param-value>
   </init-param>
 </servlet>

 <servlet-mapping>
   <servlet-name>Kaptcha</servlet-name>
   <url-pattern>/Kaptcha</url-pattern>
 </servlet-mapping>

 <!--  解决中文乱码-->
 <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>
 </filter>
 <filter-mapping>
   <filter-name>characterEncodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:context="http://www.springframework.org/schema/context"
       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/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">
<!--    开启组件扫描,只扫描controller-->
    <context:component-scan base-package="com.gjd">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
<!--    配置视图解析器对象-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/html/"/>
        <property name="suffix" value=".html"/>
    </bean>

<!--    开放静态资源解析器-->
    <mvc:default-servlet-handler/>
    
<!--    开启springmvc注解-->
    <mvc:annotation-driven/>
</beans>
关于前端如何使用验证码的操作
<!--验证码 kaptcha-->
                    <li>
                        <div class="item-content">
                            <div class="item-inner">
                                <div class="item-title label">验证码</div>
                                <input type="text" id="j_captcha" placeholder="验证码">
                                <div class="item-input">
                                    <img id="captcha_img" alt="点击更换" title="点击更换" onclick="changeVerifyCode(this)" src="../../Kaptcha">
                                </div>
                            </div>
                        </div>
                    </li>

function changeVerifyCode(img) {
	img.src = "../Kaptcha?" + Math.floor(Math.random() * 100);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值