SpringMVC入门案例
来源:互联网 发布:清华大学研究生 知乎 编辑:程序博客网 时间:2024/06/08 01:04
非注解方式
项目结构图
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--SpringMVC前端控制器配置--> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping></web-app>
dispatcher-servlet.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置Heandler--> <bean name="/queryItems.action" class="ssm.controller.ItemsController1"/> <!--处理器适配器--> <!--所有的处理器适配器都实现了HandlerAdapter接口--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!--处理器映射器--> <!--将Bean的name作为URL进行查找,需要在配置handler时指定beanname(就是url)--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!--视图解析器--> <!--配置解析jsp的视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/></beans>
ItemsController1.java
package ssm.controller;/** * Created with IntelliJ IDEA. * User: YEN * Date: 2016/8/6 * Time: 10:40 */import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import ssm.pojo.Items;import java.util.ArrayList;import java.util.List;/** * */public class ItemsController1 implements Controller{ @Override public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) 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.setAttribut 在jsp中通过itemsList取数据 modelAndView.addObject("itemsList",itemsList); //指定视图 modelAndView.setViewName("items/itemsList.jsp"); return modelAndView; }}
items.java
package ssm.pojo;import java.util.Date;/** * Created with IntelliJ IDEA. * User: YEN * Date: 2016/8/6 * Time: 10:49 */public class Items { private Integer id; private String name; private Float price; private String pic; private Date createtime; private String detail; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Float getPrice() { return price; } public void setPrice(Float price) { this.price = price; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = (detail == null ? null : detail.trim()); }}
itemsList.jsp
<%-- Created by IntelliJ IDEA. User: YEN Date: 2016/8/6 Time: 11:00 --%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%><html><head> <title>查询商品列表</title></head><body><form action="${pageContext.request.contextPath }/queryItems.action" method="post"> 查询条件: <table width="100%" border=1> <tr> <td> <input type="submit" value="查询"/></td> </tr> </table> 商品列表: <table width="100%" border=1> <tr> <td>商品名称</td> <td>商品价格</td> <td>生产日期</td> <td>商品描述</td> <td>操作</td> </tr> <c:forEach items="${itemsList }" var="item"> <tr> <td>${item.name }</td> <td>${item.price }</td> <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td> <td>${item.detail }</td> <td><a href="${pageContext.request.contextPath }/editItem.action?id=${item.id}">修改</a></td> </tr> </c:forEach> </table></form></body></html>
注解方式
dispatcher-servlet.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/tool" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--加载注解的Heandler--> <!--实际开发中建议组件扫描--> <context:component-scan base-package="ssm.controller"> </context:component-scan> <!--mvc:annotation-driven默认加载很多的参数绑定方法--> <!--<mvc:annotation></mvc:annotation>--> <!--注解适配器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> <!--注解映射器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <!--视图解析器--> <!--配置解析jsp的视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/></beans>
ItemsController3.java
package ssm.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import ssm.pojo.Items;import java.util.ArrayList;import java.util.List;/** * Created with IntelliJ IDEA. * User: YEN * Date: 2016/8/6 * Time: 13:28 *///标记它是一个注解 @Controller标识@Controllerpublic class ItemsController3 { //商品查询列表 // @RenderMapping("/queryItems")实现的是方法和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.setAttribut 在jsp中通过itemsList取数据 modelAndView.addObject("itemsList",itemsList); //指定视图 modelAndView.setViewName("items/itemsList.jsp"); return modelAndView; }}
0 0
- springmvc入门案例
- SpringMVC之入门案例
- springMvc入门案例
- SpringMvc的入门案例
- SpringMVC入门案例
- Springmvc入门案例
- springmvc入门案例
- SpringMVC入门案例
- 【SpringMVC】入门案例
- 【SpringMVC】入门案例
- SpringMVC入门案例(3)
- Springmvc入门案例(1)
- SpringMVC教程 入门案例 常见问题
- SpringMvc之入门案例-yellowcong
- springMVC学习笔记之入门案例
- SpringMVC的第一个入门案例
- springmvc入门案例可能出现的错误
- SpringMVC的第一个入门案例
- eNSP 路由器设置静态路由&net转换
- 前端优化总结
- linphone-PresenceModelImpl文件对应的JNI层文件分析
- poj2367Genealogical tree
- Android Material Design控件学习(一)——TabLayout的用法
- SpringMVC入门案例
- Jsp页面基础语法
- 数据库连接池
- iOS UI篇16- 图片压缩方法
- cdoj 1351 柱爷抢银行III
- PHP程序设计读书笔记三
- MySQL笔记-数据库约束条件
- 分类的线性方法
- 【DRP】——servlet