SpringMVC配置HelloWorld

1.配置web.xml文件:



	AweekServer1.1
	
		user
		org.springframework.web.servlet.DispatcherServlet
		1
	
	
		user

		/

	

2.在web-inf下配置servlet.xml文件:



	
	
	
	
		
		
	

3.写一个controller:

@Controller
public class UserController {
	
	/**
	 * 注意:方法必须是public属性,第一次写的时候变成了私有属性,结果老是加载不了。
	 *'/'表示的是在url-pattern中设置的路径。
	 */
	@RequestMapping("/hello")
	public void helloWorld(){
		System.out.println("helloWorld");
	}
	

}

4.运行

5.如何获取Response与request对象:

	@RequestMapping("/get")
	
		public String getRequest(HttpServletRequest request,HttpServletResponse response){
		request.setAttribute("hello", "你好");
		
		//自动映射到hello.jsp中的表达式
		return "hello";
	}

}
hello.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


hello:${hello }${string }


关于Servlet中Url正确匹配的问题:

http://www.oschina.net/question/554525_135696

 * Controller是单例的.不是线程安全的。所以不要有类的参数。

你可能感兴趣的:(java)