spring mvc 配置相关(2)

2、action-servlet.xml 配置spring的视图解析器,控制器

<!-- View Resolver 
表示将在Controller返回的ModelAndView的基础上,加上目录前缀/WEB-INF/view/,加后文件名称后缀.jsp, 
由此等待下个页面如/WEB-INF/view/listEmp.jsp 
--> 
<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/view/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

<!-- Request Mapping 
mapping属性设置中,<prop>的"key"设置的是请求的URL,"value"值为实际上要处理请求的controller的bean名称 
--> 
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
  <props> 
  <prop key="/empAction.do">empAction</prop> 
  </props> 
    </property> 
</bean> 

<!-- Spring Controller 
处理请求的控制器,该控制器继承了MultiActionController,可以定义页面请求到控制器方法名的映射 
使用这种方式,我们访问多个方法的url分别为  method=方法 
例如:http://localhost:8080/EmpTest/empAction.do?method=listEmp 
--> 
<bean id="empAction" class="com.want.action.EmpAction"> 
<property name="empDelegator" ref="empDelegator" /> 
<property name="methodNameResolver" ref="empMethodNameResolver"/> 
</bean> 

<bean id="empMethodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> 
    <property name="paramName" value="method"></property> 
</bean> 

 

controller 类

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.empmgr.delegator.EmpDelegator;

public class EmpAction extends MultiActionController {
	private EmpDelegator empDelegator;

	public void setEmpDelegator(EmpDelegator empDelegator) {
		this.empDelegator = empDelegator;
	}

	public ModelAndView listEmp(HttpServletRequest request,
			HttpServletResponse response) {
		String branchSid = request.getParameter("branchSid");
		String projectSid = request.getParameter("projectSid");
		List list = empDelegator.getEmpList(branchSid, projectSid);
		if(list.size()==0){
			System.out.println("没有信息");
		}
		ModelAndView mav=new ModelAndView("emp/listEmp", "list", list);
		return mav;
	}

}

 

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