技术标签: # Spring MVC J2EE
Controller和RequestMapping注释类型是SpringMVC API最重要的两个注释类型。
基于注解的控制器的几个优点:
一个控制器类可以控制几个动作,而一个实现了Controller接口的控制器只能处理一个动作。这就允许将相关操作写在一个控制器类内,从而减少应用类的数量
基于注解的控制器的请求映射不需要存储在配置文件中,而是使用RequestMapping注释类型,可以对一个方法进行请求处理。
下面是一个带注解的控制器
package com.springmvc.controller;
import org.springframework.stereotype.Controller;
@Controller
public class ProductController {
//request-handing method
}
Spring使用扫描机制来找到应用程序中所有基于注解的控制器类。为了保证Spring能够找到你的控制器,需要完成两件事情。
第一:在Spring MVC的配置文件中声明spring-context
<beans xmlns:context="http://www.springframework.org/schema/context" ></beans>
第二步:需要在应用
<context:component-scan base-package="控制器类的基本包"/>
在这个元素中指定控制器类的基本包位置,例如控制器类都在com.springmvc.controller及其子包下面,则按照下面的写法。
<context:component-scan base-package="com.springmvc.controller"/>
确保所有控制器类都在基本包之下,并且不要指定一个非常宽泛的包,这样会使得Spring MVC去扫描无关的包。
需要在控制器类的内部为每一个动作开发相应的处理方法,要让Spring知道用那里一种方法来处理它的动作。需要使用org.springframework.web.bind.annotation.RequestMapping注释类型映射的URI方法。
RequestMapping注释类型的作用和其名字所暗示的一样,映射一个请求和一种方法。可以使用@RequestMapping注释一种方法或类。
一个采用@RequestMapping注释的方法将成为一个请求处理的方法,并由调用程序在接收到对应的URL请求时调用。
package com.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
……….
@Controller
public class CustomerController {
@RequestMapping(value = "/customer_input")
public String inputCustomer() {
………………..
return "CustomerForm";
}
@RequestMapping(value = "/product_save", method = RequestMethod.POST)
public String saveProduct(ProductForm productForm, RedirectAttributes redirectAttributes) {
return "redirect:/product_view/" + savedProduct.getId();
}
}
Value是RequestMapping 注释的默认属性,因此下面两个标注的含义相同。
@RequestMapping(value = “/customer_input”)和@RequestMapping( “/customer_input”)
但如果超过一个属性的时候,就必须要写入value的值。
使用RequestMapping注解的value属性将URI映射到方法上。在上面将customer_input映射到inputCustomer方法上。这样可以使用如下URI访问inputCustomer方法。
http://domain/context/customer_input
RequestMapping注释类型也可以用来注释一个控制器类
package com.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@RequestMapping(value = "/delete", method ={
RequestMethod.POST, RequestMethod.PUT))
public String deleteCustomer() {
//do something here
return ……..;}
}
由于控制器的映射使用"/customer",而deleteCustomer方法映射为"/delete",则如下URL会映射到该方法上。http://domain/context/customer/delete
每一个请求处理方法可以有不同类型的参数,以及一个多钟类型的返回结果。例如,如果在请求处理方法中需要访问HttpSession对象,则可以添加的HttpSeesion作为参数,Sring会将对象正确的传递给方法
@requestMapping("/url")
public String myMethod(HttpSession session){
...........
session.addAttribute(key,value);
...........
}
或者,如果需要访问客户端语言环境和HttpServletRequest对象,则可以在方法上签名上包括这样的参数
@requestMapping("/url")
public String myOtherMethod(HttpServletRequest request,Locale locale){
...........
// access Locale and HttpServletRequest here
...........
}
package com.springmvc.domain;
import java.io.Serializable;
public class Product implements Serializable {
private static final long serialVersionUID = 5784L;
private long id;
private String name;
private String description;
private float price;
public long getId() {
return id;}
public void setId(long id) {
this.id = id;}
public String getName() {
return 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;}
}
package com.springmvc.form;
public class ProductForm {
private String name;
private String description;
private String price;
public String getName() {
return 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;}
}
package com.springmvc.controller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.springmvc.domain.Product;
import com.springmvc.form.ProductForm;
@Controller
public class ProductController {
private static final Log logger = LogFactory.getLog(ProductController.class);
@RequestMapping(value="/product_input")
public String inputProduct() {
logger.info("inputProduct called");
return "ProductForm";
}
@RequestMapping(value="/product_save")
public String saveProduct(ProductForm productForm, Model model) {
logger.info("saveProduct called");
// no need to create and instantiate a ProductForm
// create Product
Product product = new Product();
product.setName(productForm.getName());
product.setDescription(productForm.getDescription());
try {
product.setPrice(Float.parseFloat(
productForm.getPrice()));
} catch (NumberFormatException e) {
}
// add product
model.addAttribute("product", product);
return "ProductDetails";
}
}
<?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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.springmvc.controller"/>
<mvc:annotation-driven/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/*.html" location="/"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
#global {
text-align: left; border: 1px solid #dedede;background: #efefef;
width: 560px;padding: 20px;margin: 100px auto;}
form {
font:100% verdana;min-width: 500px;max-width: 600px;width: 560px;}
form fieldset {
border-color: #bdbebf;border-width: 3px;margin: 0;}
legend {
font-size: 1.3em;}
form label {
width: 250px;display: block;float: left;text-align: right;padding: 2px;}
#buttons {
text-align: right;}
#errors, li {
color: red;}
<!DOCTYPE HTML>
<html>
<head>
<title>Add Product Form</title>
<style type="text/css">@import url(css/main.css);</style>
</head>
<body>
<div id="global">
<form action="product_save" method="post">
<fieldset>
<legend>Add a product</legend>
<p>
<label for="name">Product Name: </label>
<input type="text" id="name" name="name" tabindex="1">
</p>
<p>
<label for="description">Description: </label>
<input type="text" id="description" name="description" tabindex="2">
</p>
<p>
<label for="price">Price: </label>
<input type="text" id="price" name="price" tabindex="3">
</p>
<p id="buttons">
<input id="reset" type="reset" tabindex="4">
<input id="submit" type="submit" tabindex="5" value="Add Product">
</p>
</fieldset>
</form>
</div>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<title>Save Product</title>
<style type="text/css">@import url(css/main.css);</style>
</head>
<body>
<div id="global">
<h4>The product has been saved.</h4>
<p>
<h5>Details:</h5>
Product Name: ${
product.name}<br/>
Description: ${
product.description}<br/>
Price: $${
product.price}
</p>
</div>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>YJYSpring_MVC_Annotation_PaulDeckCH04</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
http://localhost:8080/YJYSpring_MVC_Annotation_PaulDeckCH04/product_input.action
文章浏览阅读90次。【代码】js-选项卡原理。_选项卡js原理
文章浏览阅读67次。原型模式是一种对象创建型模式,它采用复制原型对象的方法来创建对象的实例。它创建的实例,具有与原型一样的数据结构和值分为深度克隆和浅度克隆。浅度克隆:克隆对象的值类型(基本数据类型),克隆引用类型的地址;深度克隆:克隆对象的值类型,引用类型的对象也复制一份副本。UML图:具体代码:浅度复制:import java.util.List;/*..._prototype 设计模式
文章浏览阅读59次。入选国内首批云计算服务创新发展试点城市的北京、上海、深圳、杭州和无锡起到了很好的示范作用,不仅促进了当地产业的升级换代,而且为国内其他城市发展云计算产业提供了很好的借鉴。据了解,目前国内至少有20个城市确定将云计算作为重点发展的产业。这势必会形成新一轮的云计算基础设施建设的**。由于云计算基础设施建设具有投资规模大,运维成本高,投资回收周期长,地域辐射性强等诸多特点,各地在建...
文章浏览阅读9.4k次,点赞2次,收藏20次。一、功能及目的 在每个STM32的芯片上都有两个管脚BOOT0和BOOT1,这两个管脚在芯片复位时的电平状态决定了芯片复位后从哪个区域开始执行程序。BOOT1=x BOOT0=0 // 从用户闪存启动,这是正常的工作模式。BOOT1=0 BOOT0=1 // 从系统存储器启动,这种模式启动的程序_stm32boot0和boot1作用
文章浏览阅读3.4k次,点赞2次,收藏22次。C语言函数递归调用_c语言函数递归调用
文章浏览阅读410次。明日方舟bilibili服是一款天灾驾到战斗热血的创新二次元废土风塔防手游,精妙的二次元纸片人设计,为宅友们源源不断更新超多的纸片人老婆老公们,玩家将扮演废土正义一方“罗德岛”中的指挥官,与你身边的感染者们并肩作战。与同类塔防手游与众不同的几点,首先你可以在这抽卡轻松获得稀有,同时也可以在战斗体系和敌军走位机制看到不同。明日方舟bilibili服设定:1、起因不明并四处肆虐的天灾,席卷过的土地上出..._明日方舟抽卡模拟器
文章浏览阅读437次。Maven上传Jar到私服报错:ReasonPhrase: Repository version policy: SNAPSHOT does not allow version: xxx_repository version policy snapshot does not all
文章浏览阅读1.2k次。斐波那契数列(Fibonacci Sequence)是由如下形式的一系列数字组成的:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …上述数字序列中反映出来的规律,就是下一个数字是该数字前面两个紧邻数字的和,具体如下所示:示例:比如上述斐波那契数列中的最后两个数,可以推导出34后面的数为21+34=55下面是一个更长一些的斐波那契数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584,_斐波那契日
文章浏览阅读363次。PHP必会面试题1. 基础篇1. 用 PHP 打印出前一天的时间格式是 2017-12-28 22:21:21? //>>1.当前时间减去一天的时间,然后再格式化echo date('Y-m-d H:i:s',time()-3600*24);//>>2.使用strtotime,可以将任何字符串时间转换成时间戳,仅针对英文echo date('Y-m-d H:i:s',str..._//该层循环用来控制每轮 冒出一个数 需要比较的次数
文章浏览阅读1.3k次,点赞26次,收藏26次。windows下用mingw编译opencv貌似不支持cuda,选cuda会报错,我无法解决,所以没选cuda,下面两种编译方式支持。打开cmake gui程序,在下面两个框中分别输入opencv的源文件和编译目录,build-mingw为你创建的目录,可自定义命名。1、如果已经安装Qt,则Qt自带mingw编译器,从Qt安装目录找到编译器所在目录即可。1、如果已经安装Qt,则Qt自带cmake,从Qt安装目录找到cmake所在目录即可。2、若未安装Qt,则安装Mingw即可,参考我的另外一篇文章。_opencv mingw contrib
文章浏览阅读10w+次,点赞42次,收藏309次。今天给大家推荐5个好用且免费的简历模板网站,简洁美观,非常值得收藏!1、菜鸟图库https://www.sucai999.com/search/word/0_242_0.html?v=NTYxMjky网站主要以设计类素材为主,办公类素材也很多,简历模板大部个偏简约风,各种版式都有,而且经常会更新。最重要的是全部都能免费下载。2、个人简历网https://www.gerenjianli.com/moban/这是一个专门提供简历模板的网站,里面有超多模板个类,找起来非常方便,风格也很多样,无须注册就能免费下载,_hoso模板官网
文章浏览阅读142次。你听说过吗?该计划可让您以推广您的产品并在成功销售时支付佣金。它提供了新的营销渠道,使您的产品呈现在更广泛的受众面前并提高品牌知名度。此外,TikTok Shop联盟可以是一种经济高效的产品或服务营销方式。您只需在有人购买时付费,因此不存在在无效广告上浪费金钱的风险。这些诱人的好处是否足以让您想要开始您的TikTok Shop联盟活动?如果是这样,本指南适合您。_tiktok联盟