Spring MVC-註解

基於spring註解的MVC


1.創建web項目

2.引入類庫

com.springsource.javax.annotation-1.0.0.jar
com.springsource.javax.servlet.jsp.jstl-1.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.taglibs.standard-1.1.2.jar
org.springframework.aop-3.0.0.RELEASE.jar
org.springframework.asm-3.0.0.RELEASE.jar
org.springframework.beans-3.0.0.RELEASE.jar
org.springframework.context.support-3.0.0.RELEASE.jar
org.springframework.context-3.0.0.RELEASE.jar
org.springframework.core-3.0.0.RELEASE.jar
org.springframework.expression-3.0.0.RELEASE.jar
org.springframework.web.servlet-3.0.0.RELEASE.jar
org.springframework.web-3.0.0.RELEASE.jar

3.在web.xml文件中配置DispatcherServlet

	
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
 


4.創建控制器HomeController

public class Person {

	private String id;
	private String name;
	private String address;
	/**
	 * @return the id
	 */
	public final String getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public final void setId(String id) {
		System.out.println("正在調用setId方法 ,id=" + id);
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public final String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public final void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the address
	 */
	public final String getAddress() {
		return address;
	}
	/**
	 * @param address the address to set
	 */
	public final void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "{id:"+id+",name:"+name+",address:"+address+"}";
	}
}

@Controller
@RequestMapping(value="/home")//根路徑
public class HomeController {
	//訪問路徑 //www.xxx.com/根路徑/子路徑
	@RequestMapping(value="/a",method=RequestMethod.GET)//子路徑
	public String method1(@RequestParam(value="id") String uid,String name,String address,Person p,HttpServletRequest req,Map<String, Object> model){
		//uid = req.getParameter("id");
		System.out.println("---------method1---------id=" + uid + " name=" + name + " address=" + address);
		System.out.println("person:" + p);
		
		req.setAttribute("msg", "hello world");
		model.put("key", "value");
		return "home";//邏輯名
	}
	
	@RequestMapping(value="/b")
	public String method2(){
		return "home";
	}

}

5.創建spring配置文件springmvc.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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
						http://www.springframework.org/schema/mvc 
						http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
						http://www.springframework.org/schema/context 
						http://www.springframework.org/schema/context/spring-context-3.0.xsd 
						http://www.springframework.org/schema/aop 
						http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
	
	<!-- 註解驅動 -->
	<mvc:annotation-driven/>
	
	<!-- 組件掃描 -->
	<context:component-scan base-package="cn.itcast.springmvc.controller"></context:component-scan>
	
	<!-- 視圖解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前綴 /WEB-INF/jsps/home.jsp-->
		<property name="prefix" value="/WEB-INF/jsps/"></property>
		<!-- 後綴 -->
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>


6.創建jsp./WEB-INF/jsp/hello.jsp



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章