Skip to content

Commit

Permalink
注解和非注解方式
Browse files Browse the repository at this point in the history
  • Loading branch information
eson15 committed Jun 17, 2016
1 parent cd85b48 commit 3cb327a
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 11 deletions.
1 change: 1 addition & 0 deletions WebRoot/WEB-INF/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/classes/
52 changes: 47 additions & 5 deletions WebRoot/WEB-INF/classes/springmvc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,61 @@
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 配置Handler -->
<!-- 配置Handler
<bean name="/queryItems.action" class="ssm.controller.ItemsController1" />
<bean id="itemsController1" class="ssm.controller.ItemsController1" />
<bean id="itemsController2" class="ssm.controller.ItemsController2" />
-->

<!-- 处理器适配器:HandlerAdapter
所有处理器适配器都实现HandlerAdapter接口
-->
<!-- SimpleControllerHandlerAdapter适配器能执行实现了Controller接口的Handler -->
<!-- 2. HttpRequestHandlerAdapter适配器能执行实现了HttpRequestHandler接口的Handler
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
-->

<!-- 1. SimpleControllerHandlerAdapter适配器能执行实现了Controller接口的Handler
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
-->

<!-- 配置处理器映射器
将bean的name作为url进行查找,需要在配置Handler时指定beanname(就是url)
-->

<!-- 配置处理器映射器1:将bean的name作为url进行查找,需要在配置Handler时指定beanname(就是url)
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
-->

<!-- 配置处理器映射器2,简单url映射
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props> -->
<!-- 对itemsController1进行url映射,url是/queryItems1.action -->
<!--
<prop key="/queryItems1.action">itemsController1</prop>
<prop key="/queryItems2.action">itemsController2</prop>
</props>
</property>
</bean>
-->

<!--
<bean class="ssm.controller.ItemsController3"/>
-->

<!-- 使用注解的映射器和注解的适配器。(注解的映射器和注解的适配器必须配对使用)-->
<!-- 配置注解适配器
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
-->
<!-- 配置注解映射器
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
-->

<!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,
如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发时使用mvc:annotation-driven
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 也不用配置具体的Controller了,直接使用这个进行扫描包即可,也可以扫描service -->
<context:component-scan base-package="ssm.controller"></context:component-scan>

<!-- 配置视图解析器
进行jsp解析,默认使用jstl标签,classpath下得有jstl的包
Expand Down
Binary file modified WebRoot/WEB-INF/classes/ssm/controller/ItemsController1.class
Binary file not shown.
52 changes: 47 additions & 5 deletions config/springmvc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,61 @@
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 配置Handler -->
<!-- 配置Handler
<bean name="/queryItems.action" class="ssm.controller.ItemsController1" />
<bean id="itemsController1" class="ssm.controller.ItemsController1" />
<bean id="itemsController2" class="ssm.controller.ItemsController2" />
-->

<!-- 处理器适配器:HandlerAdapter
所有处理器适配器都实现HandlerAdapter接口
-->
<!-- SimpleControllerHandlerAdapter适配器能执行实现了Controller接口的Handler -->
<!-- 2. HttpRequestHandlerAdapter适配器能执行实现了HttpRequestHandler接口的Handler
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
-->

<!-- 1. SimpleControllerHandlerAdapter适配器能执行实现了Controller接口的Handler
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
-->

<!-- 配置处理器映射器
将bean的name作为url进行查找,需要在配置Handler时指定beanname(就是url)
-->

<!-- 配置处理器映射器1:将bean的name作为url进行查找,需要在配置Handler时指定beanname(就是url)
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
-->

<!-- 配置处理器映射器2,简单url映射
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props> -->
<!-- 对itemsController1进行url映射,url是/queryItems1.action -->
<!--
<prop key="/queryItems1.action">itemsController1</prop>
<prop key="/queryItems2.action">itemsController2</prop>
</props>
</property>
</bean>
-->

<!--
<bean class="ssm.controller.ItemsController3"/>
-->

<!-- 使用注解的映射器和注解的适配器。(注解的映射器和注解的适配器必须配对使用)-->
<!-- 配置注解适配器
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
-->
<!-- 配置注解映射器
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
-->

<!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
mvc:annotation-driven默认加载很多的参数绑定方法,比如json转换解析器就默认加载了,
如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发时使用mvc:annotation-driven
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 也不用配置具体的Controller了,直接使用这个进行扫描包即可,也可以扫描service -->
<context:component-scan base-package="ssm.controller"></context:component-scan>

<!-- 配置视图解析器
进行jsp解析,默认使用jstl标签,classpath下得有jstl的包
Expand Down
2 changes: 1 addition & 1 deletion src/ssm/controller/ItemsController1.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ModelAndView handleRequest(HttpServletRequest request,
ModelAndView modelAndView = new ModelAndView();

//相当于request的setAttribute,在jsp页面中通过itemsList取数据
modelAndView.addObject("itemaList", itemsList);
modelAndView.addObject("itemsList", itemsList);

//指定视图
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
Expand Down
49 changes: 49 additions & 0 deletions src/ssm/controller/ItemsController2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ssm.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.HttpRequestHandler;


import ssm.po.Items;

public class ItemsController2 implements HttpRequestHandler {

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//实际中是调用service查找数据库,查询商品列表,这里直接使用静态数据来模拟了
List<Items> itemsList = new ArrayList<Items>();
//向list中填充静态数据
Items items_1 = new Items();
items_1.setName("联想笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 联想笔记本电脑!");

Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iPhone6苹果手机!");

itemsList.add(items_1);
itemsList.add(items_2);

//设置模型数据
request.setAttribute("itemsList", itemsList);

//设置转发的视图
request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request, response);

//使用此方法可以通过修改response,设置响应的数据格式,比如响应json数据
/*
response.setCharacterEncoding("utf-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().write("json串");*/
}
}
79 changes: 79 additions & 0 deletions src/ssm/controller/ItemsController3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package ssm.controller;

import java.util.ArrayList;
import java.util.List;

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

import ssm.po.Items;

@Controller
public class ItemsController3 {

// 商品查询列表
// @RequestMapping实现 对queryItems方法和url进行映射,一个方法对应一个url
// 一般建议将url和方法写成一样
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception {

// 实际中是调用service查找数据库,查询商品列表,这里直接使用静态数据来模拟了
List<Items> itemsList = new ArrayList<Items>();
// 向list中填充静态数据
Items items_1 = new Items();
items_1.setName("联想笔记本");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 联想笔记本电脑!");

Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iPhone6苹果手机!");

itemsList.add(items_1);
itemsList.add(items_2);

// 返回ModelAndView
ModelAndView modelAndView = new ModelAndView();

// 相当于request的setAttribute,在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList", itemsList);

// 指定视图
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

return modelAndView;
}

@RequestMapping("/queryItems2")
public ModelAndView queryItems2() throws Exception {

// 实际中是调用service查找数据库,查询商品列表,这里直接使用静态数据来模拟了
List<Items> itemsList = new ArrayList<Items>();
// 向list中填充静态数据
Items items_1 = new Items();
items_1.setName("联想笔记本2");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 联想笔记本电脑!");

Items items_2 = new Items();
items_2.setName("苹果手机2");
items_2.setPrice(5000f);
items_2.setDetail("iPhone6苹果手机!");

itemsList.add(items_1);
itemsList.add(items_2);

// 返回ModelAndView
ModelAndView modelAndView = new ModelAndView();

// 相当于request的setAttribute,在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList", itemsList);

// 指定视图
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

return modelAndView;
}
}

0 comments on commit 3cb327a

Please sign in to comment.