springmvc程序案例1

1创建一个web工程

springmvc程序案例1_第1张图片
2导入springmvc的jar文件

springmvc程序案例1_第2张图片

3在web.xml中配置前端控制器 

  
  	springmvc
  	org.springframework.web.servlet.DispatcherServlet
 
  
  	contextConfigLocation
  	classpath:springmvc.xml
  
  
  
  
  	springmvc
  	*.do
  


4在springmvc.xml文件中配置处理器映射器




6自定义类UserController

package cn.itcast.controller;

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{

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mvAndView = new ModelAndView() ;
		mvAndView.addObject("hello", "I love too");
		//
		//mvAndView.setViewName("/WEB-INF/jsps/index.jsp");
		mvAndView.setViewName("index");
		return mvAndView;
	}	
}



创建的页面


${hello}




7配置自定义UserController的bean


                           
  


8配置视图解析器:

如果controller使用逻辑视图,必须配置视图解析器
 
     
     
	


9加载springmvc.xml

  
  	contextConfigLocation
  	classpath:springmvc.xml
  


10访问 项目名+自定义controller的Bean的name属性的url

http://localhost:8080/springmvc1/hello.do


你可能感兴趣的:(java相关)