Spring MVC入门(登录示例)

一 环境: Myeclipse6.6+JDK1.5+Tomcat6
二 开发框架: spring3
  所用jar文件
   Spring MVC入门(登录示例)
三 代码示例:
1  XML配置文件代码:
Web.xml

   
<filter>
		<filter-name>encodingFilter</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>encodingFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>	
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>


springmvc-servlet.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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- Default ViewResolver -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp"></property>
	</bean>    	
    
     <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/user.do">userController</prop>
            </props>
        </property>
    </bean>
    
     <bean id="userController" class="net.liuzd.web.action.UserController">	
        	<property name="userId">
        	   <value>spring</value>
        	</property>
        	<property name="userPassword">
        	   <value>spring</value>
        	</property>
	</bean>  
	
 </beans>


Java代码

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class UserController implements Controller {
	
	private String userId = null;
	private String userPassword = null;	

	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserPassword() {
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}	
	public ModelAndView handleRequest(HttpServletRequest rq,
			HttpServletResponse resp) throws Exception {
		String _userId = rq.getParameter("userId");
		String _userPassword = rq.getParameter("userPassword");		
		rq.setAttribute("userId", _userId);
		if(userId.equals(_userId) && userPassword.equals(_userPassword)){			
			return new ModelAndView("success");
		}
		rq.setAttribute("userPassword", _userPassword);
		return new ModelAndView("error");
	}	
}

四 注意事项
在web.xml配置文件中配置中的
<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

这个servlet名称必须和WEB-INF/下的springmvc-servlet.xml前缀名称一致.spring容器在加载web.xml文件时,会默认寻找位于WEB-INF/下以springmvc为前缀的spring配置文件.

Spring3 MVC Annotaction入门(登录示例)
http://liuzidong.iteye.com/blog/899420

你可能感兴趣的:(java,spring,mvc,Web,servlet)