日期:2014-05-16  浏览次数:20403 次

SpringMVC和JSP 登录实例 相关配置

SpringMVC + Jsp

?

需要的jar包:

?

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">
    <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/myConfig.xml</param-value>
      </init-param>
    </servlet>
  
    <servlet-mapping>
       <servlet-name>dispatcher</servlet-name>
       <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

?

?

如果没有指定 init-param参数,默认为src路径下dispatcher-servlet.xml文件,即(servlet-name)-servlet.xml配置。

?

myConfig.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:drools="http://drools.org/schema/drools-spring"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://drools.org/schema/drools-spring classpath:org/drools/container/spring/drools-spring-1.0.0.xsd">
       
       <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass">
                <value>org.springframework.web.servlet.view.JstlView</value>
            </property>
            <property name="prefix">
                <value>/WEB-INF/view/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
       </bean>
       
       <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                  <prop key="/login.do">LoginAction</prop>
                </props>
            </property>
       </bean>
       
       <bean id="LoginAction" class="com.hammer.login.LoginAction">
            <property name="commandClass">
               <value>com.hammer.login.Log