五、SpringMVC注解的开发(十分简单)

首先在spring.xml中,只需要使用

<?xml version="1.0" encoding="UTF-8"?>

-<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">

<!-- 扫描@Controler @Service -->


<context:component-scan base-package="cn.xwb"/>

</beans>

经过扫描即可直接找到相应的Control类
Control类

package cn.xwb;

import java.util.ArrayList;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.item.*;


@Controller
public class ItemController {
	@RequestMapping(value="/i.action")
	public ModelAndView intemList(){
		// 创建页面需要显示的商品数据
		List<Items> list = new ArrayList<Items>();
		list.add(new Items(1, "1华为 荣耀8", 2399f, new Date(), "质量好!1"));
		list.add(new Items(2, "2华为 荣耀8", 2399f, new Date(), "质量好!2"));
		list.add(new Items(3, "3华为 荣耀8", 2399f, new Date(), "质量好!3"));
		list.add(new Items(4, "4华为 荣耀8", 2399f, new Date(), "质量好!4"));
		list.add(new Items(5, "5华为 荣耀8", 2399f, new Date(), "质量好!5"));
		list.add(new Items(6, "6华为 荣耀8", 2399f, new Date(), "质量好!6"));

		ModelAndView mav = new ModelAndView();
		//数据
		mav.addObject("itemList", list);
		mav.setViewName("/WEB-INF/jsp/itemList.jsp");
		return mav;
	}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>

-<web-app version="2.5" id="WebApp_ID" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<display-name>springmvc-01</display-name>


-<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>


-<servlet>

<servlet-name>mvc-dispatcher</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>

<load-on-startup>1</load-on-startup>

</servlet>


-<servlet-mapping>

<servlet-name>mvc-dispatcher</servlet-name>

<url-pattern>*.action</url-pattern>

</servlet-mapping>

</web-app>

因此使用注解开发十分简单,不需要十分复杂的适配器和映射器,下一阶段是利用SpringMVC整合Mybatis,很难,但是我会坚持,学完整合就是Spring cloud和Spring boot,然后是安卓,IOS…学习,这中间可能会学习maven,加油啦。

猜你喜欢

转载自blog.csdn.net/qq_34446716/article/details/82779359