spring MVC

第一步:在web.xml文件中加入<listener>和<context-param>。并且加入spirng 的DispatcherServlet。

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring-dao.xml,/WEB-INF/spring-service.xml</param-value>
	</context-param>
	
	
	<servlet>
		<servlet-name>contractManager</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>contractManager</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

 上面serlvet的名字用来命名【servlet-name】-servlet.xml。

 

 

第二步:  按上面的xxx-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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
				<prop key="/check.do">userController</prop>
			</props>
		</property>
	</bean>

	<bean id="userController" class="com.vanceinfo.controller.UserController">
		<property name="userService">
			<ref bean="userService"/>
		</property>
		<property name="methodNameResolver">
			<ref local="methodNameResolver"/>
		</property>
	</bean>
	
	<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
		<property name="mappings">
			<props>
				<prop key="/check.do">getAll</prop><!-- 调用的具体方法。 -->
			</props>
		</property>
	</bean>

	<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
		<property name="basename">
			<value>views-springMVC</value><!-- 返回跳转页面的资源文件。 -->
		</property>
	</bean>

</beans>

 

 

第三步:

流程处理内容:

package com.vanceinfo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.vanceinfo.service.IUserService;
import com.vanceinfo.service.impl.UserServiceImpl;

public class UserController extends MultiActionController {

	private IUserService userService;
	private ModelAndView mav; //返回的信息封装
	
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	public ModelAndView getAll(HttpServletRequest req,HttpServletResponse res){
		
		String username = req.getParameter("username");
		
		if("admin".equals(username)){
			List list = 	userService.getAll();
			Map map = new HashMap();
			map.put("list", list);
			
			mav = new ModelAndView("show",map);
		}else{
			mav = new ModelAndView("error"); 
		}
		
		return mav;
	}
}

 

 

第四步 : 加入页面跳转资源文件:

show.class=org.springframework.web.servlet.view.InternalResourceView
show.url=/success.jsp
error.class=org.springframework.web.servlet.view.InternalResourceView
error.url=/error.jsp

 

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