欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring3 MVC 初体验

程序员文章站 2022-07-08 10:24:58
...

 

commons-logging-1.0.4.jar
jstl-1.2.jar
log4j-1.2.16.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
standard.jar

 

<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><!-- springmvc 要与后面的配置文档名字前缀一致 -->
		<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>

 

 

<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"/>
   </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="mvc.action.UserController">
	   	<property name="userId">
	   		<value>spring</value>
	   	</property>
	   	<property name="userPassword">
	   		<value>spring</value>
	   	</property>
   	
   </bean>

 

 

 登录错误:  ID号: ${userId},密码: ${userPassword}<br>
     <a href="${pageContext.request.contextPath}/index.jsp">返回</a>
 
 欢迎你: ${userId}<br>
     <a href="${pageContext.request.contextPath}/index.jsp">返回</a>

 

class UserController implements Controller{
  private String userId = null;
  private String userPassword = null;
//....省略掉 set get
  ModelAndView handleRequest(){
        String _userId = request.getParameter("userId");
	String _userPassword = request.getParameter("userPassword");
	request.setAttribute("userId", _userId);
	if ( userId.equals(_userId) && 
                              userPassword.equals(_userPassword)) {
			return new ModelAndView("success");
		}
	request.setAttribute("userPassword", _userPassword);
	return new ModelAndView("error");
    }
}

上一篇: JAVA注解

下一篇: spring3 mvc 注解