Java EE - Spring MVC 入门介绍以及基于注解开发应用

Java EE - Spring MVC 入门介绍以及基于注解开发应用,第1张

概述问题导读:1. Spring MVC 的入门 *** 作2. 如何基于注解开发Spring MVC应用?解决方案:Spring MVC入门介绍

问题导读:

1. Spring MVC 的入门 *** 作

2. 如何基于注解开发Spring MVC利用?


解决方案:


Spring MVC入门介绍
1. jar




2.bean(model)
package bean;public class Product { private String name; private String description; private float price; public String getname() { return this.name; } public voID setname(String name) { this.name = name; } public String getDescription() { return description; } public voID setDescription(String description) { this.description = description; } public float getPrice() { return price; } public voID setPrice(float price) { this.price = price; }}
3.form
package form;public class ProductForm { private String name; private String description; private String price; public String getname() { return this.name; } public voID setname(String name) { this.name = name; } public String getDescription() { return description; } public voID setDescription(String description) { this.description = description; } public String getPrice() { return price; } public voID setPrice(String price) { this.price = price; }}
4.controller
4.1 inputProductController
package com.lpl.controller;@R_419_5565@ javax.servlet.http.httpServletRequest;@R_419_5565@ javax.servlet.http.httpServletResponse;@R_419_5565@ org.springframework.web.servlet.ModelAndVIEw;@R_419_5565@ org.springframework.web.servlet.mvc.Controller;/* * 这是传统风格的控制器,实现Controller接口 */public class inputProductController implements Controller{ @OverrIDe public ModelAndVIEw handleRequest(httpServletRequest request,httpServletResponse response) throws Exception { // Todo auto-generated method stub return new ModelAndVIEw("ProductForm"); } }
4.2 SaveProductController
package com.lpl.controller;@R_419_5565@ javax.servlet.http.httpServletRequest;@R_419_5565@ javax.servlet.http.httpServletResponse;@R_419_5565@ org.springframework.web.servlet.ModelAndVIEw;@R_419_5565@ org.springframework.web.servlet.mvc.Controller;@R_419_5565@ bean.Product;@R_419_5565@ form.ProductForm;public class SaveProductController implements Controller{ @OverrIDe public ModelAndVIEw handleRequest(httpServletRequest request,httpServletResponse response) throws Exception { /* * 没有过滤器,手动设置request中对象的字符编码 */ request.setCharacterEnCoding("UTF⑻"); // 数据绑定(spring MVC 提供了该功能,以后会使用) ProductForm productForm = new ProductForm(); productForm.setname(request.getParameter("name")); productForm.setDescription(request.getParameter("description")); productForm.setPrice(request.getParameter("price")); Product product = new Product(); product.setname(productForm.getname()); product.setDescription(productForm.getDescription()); try { product.setPrice(float.parsefloat(productForm.getPrice())); } catch (NumberFormatException e) { e.printstacktrace(); } /* * 1. 视图(Jsp) * 2. 模型(名字) * 3. 模型(对象) */ return new ModelAndVIEw("ProductDetails","product",product); } }
5. 配置文件
5.1 springmvc-config<?xml version="1.0" enCoding="UTF⑻"?><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"> <!-- 配置bean --> <!-- 通过配置bean方式得到的controller每个controller就是1个action --> <bean name="/product_input.do" class="com.lpl.controller.inputProductController" /> <bean name="/product_save.do" class="com.lpl.controller.SaveProductController" /> <!-- VIEwResolver(视图解析器) 配置前缀和后缀两个属性,这样视图解析器将会自动增加前缀和后缀 --> <bean ID="vIEwResolver" class="org.springframework.web.servlet.vIEw.InternalResourceVIEwResolver"> <property name="prefix" value="/WEB-INF/Jsp/" /> <property name="suffix" value=".Jsp" /> </bean></beans>


5.2 web<?xml version="1.0" enCoding="UTF⑻"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" ID="WebApp_ID" version="2.5"> <servlet> <servlet-name>springmvc</servlet-name> <!-- dispatcherServlet 1. 调用controller中相应的action 2. 根据要求参数值构造表单bean 3. 转向另外一个vIEw层(Jsp) --> <servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class> <!-- spring MVC 的配置文件 位置 --> <init-param> <param-name>contextConfigLocation</param-name> <!-- web application的classpath包括 WEB-INF/lib下的所有jar包和WEB-INF/classes目录 --> <param-value>classpath:conf/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping></web-app>


6. vIEw
6.1 ProductForm
<%@ page language="java" ContentType="text/HTML; charset=UTF⑻" pageEnCoding="UTF⑻"%><!DOCTYPE HTML PUBliC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/HTML4/loose.dtd"><HTML><head><Meta http-equiv="Content-Type" content="text/HTML; charset=UTF⑻"><Title>添加商品</Title></head><body> <div> <form action="product_save.do" method="post"> <fIEldset> <legend>添加商品</legend> <label for="name">商品名字:</label> <input type="text" ID="name" name="name" value="" /><br/> <label for="description">商品描写:</label> <input type="text" ID="description" name="description" value="" /><br/> <label for="price">商品价格:</label> <input type="text" ID="price" name="price" value=""/><br/> <div> <input ID="reset" type="reset" /> <input ID="submit" type="submit" value="添加商品" /> </div> </fIEldset> </form> </div></body></HTML>


6.2 ProductForm
<%@ page language="java" ContentType="text/HTML; charset=UTF⑻" pageEnCoding="UTF⑻"%><!DOCTYPE HTML PUBliC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/HTML4/loose.dtd"><HTML><head><Meta http-equiv="Content-Type" content="text/HTML; charset=UTF⑻"><Title>商品详情</Title></head><body> <div> <h4>该商品已保存</h4> <p> <h5>详细信息</h5> 商品名称:${product.name }</br> 商品描写:${product.description }</br> 商品价格:${product.price } </p> </div></body></HTML>



基于注解的控制器
1. 采取注解开发利用的几个优点1个控制器类可以处理多个动作1个动作对应1个控制器类中的1个方法
2. 配置文件
2.1 web
<?xml version="1.0" enCoding="UTF⑻"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" ID="WebApp_ID" version="2.5"> <servlet> <servlet-name>springmvc</servlet-name> <!-- dispatcherServlet 1. 调用controller中相应的action 2. 根据要求参数值构造表单bean 3. 转向另外一个vIEw层(Jsp) --> <servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class> <!-- spring MVC 的配置文件 位置 --> <init-param> <param-name>contextConfigLocation</param-name> <!-- web application的classpath包括 WEB-INF/lib下的所有jar包和WEB-INF/classes目录 --> <param-value>classpath:conf/springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 1. 当URL模式设置为“/” 时,意味着所有要求(包括静态资源)1同映照到dispatcher servlet 2. 为了正确处理静态资源,需要在Spring MVC配置文件中添加<resources/>元素 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 字符过滤器 1. 处理数据绑定中文乱码 --> <filter> <filter-name>SpringEnCodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEnCodingFilter</filter-class> <init-param> <param-name>enCoding</param-name> <param-value>UTF⑻</param-value> </init-param> <init-param> <param-name>forceEnCoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapPing> <filter-name>SpringEnCodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapPing></web-app>
2.2 springmvc-config
<?xml version="1.0" enCoding="UTF⑻"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc⑶.2.xsd"> <!-- 1. 声明扫描机制 xmlns:context="http://www.springframework.org/schema/context" 2. 扫描包要注意不要指定1个太广泛的基本包 --> <context:component-scan base-package="com.lpl.controller" /> <!-- 1. 如果没有<annotation-driven/>,<resources/>元素会禁止任意控制器被调用 2. 若不需要使用 resources,则不需要<annotation-driven/>元素 --> <mvc:annotation-driven /> <!-- 指定.HTML 资源不通过dispatcher servlet --> <mvc:resources mapPing="/*.HTML" location="/" /> <!-- VIEwResolver(视图解析器) 配置前缀和后缀两个属性,这样视图解析器将会自动增加前缀和后缀 --> <bean ID="vIEwResolver" class="org.springframework.web.servlet.vIEw.InternalResourceVIEwResolver"> <property name="prefix" value="/WEB-INF/Jsp/" /> <property name="suffix" value=".Jsp" /> </bean></beans>
3. controller
package com.lpl.controller;@R_419_5565@ javax.servlet.http.httpServletRequest;@R_419_5565@ org.springframework.stereotype.Controller;@R_419_5565@ org.springframework.ui.Model;@R_419_5565@ org.springframework.web.bind.annotation.RequestMapPing;@R_419_5565@ org.springframework.web.bind.annotation.RequestMethod;@R_419_5565@ bean.Product;@R_419_5565@ form.ProductForm;/* * 1. 在配置好spring 的扫描机制的条件下,可使用注解开发利用 * 2. @RequestMapPing("/xxx.do") 与 @RequestMapPing(value = "/xxx.do") 相同 * 3. @RequestMapPing 注解总还可使用其他属性 * 4. 在编写要求处理方法的时候,可以直接在方法参数中添加httpServletRequest、httpServletResponse 等 * 5. 返回类型可以为ModelAndVIEw、Model、Map 和 String(逻辑视图名)等 */@Controllerpublic class ProductController { @RequestMapPing(value="/product_input.do") public String inputProduct() { return "ProductForm"; } @RequestMapPing(value="/product_save.do",method=RequestMethod.POST) public String saveProduct(httpServletRequest request,ProductForm productForm,Model model) { Product product = new Product(); product.setname(productForm.getname()); product.setDescription(productForm.getDescription()); try { product.setPrice(float.parsefloat(productForm.getPrice())); } catch (NumberFormatException e) { e.printstacktrace(); } //将商品加入 model 中 model.addAttribute("product",product); return "ProductDetails"; }}
4. vIEw
4.1 


4.2 



总结

以上是内存溢出为你收集整理的Java EE - Spring MVC 入门介绍以及基于注解开发应用全部内容,希望文章能够帮你解决Java EE - Spring MVC 入门介绍以及基于注解开发应用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1015883.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存