技术标签: # 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
文章浏览阅读451次。dev/mem: 物理内存的全镜像。可以用来访问物理内存。/dev/kmem: kernel看到的虚拟内存的全镜像。可以用来访问kernel的内容。调试嵌入式Linux内核时,可能需要查看某个内核变量的值。/dev/kmem正好提供了访问内核虚拟内存的途径。现在的内核大都默认禁用了/dev/kmem,打开的方法是在 make menuconfig中选中 device drivers --> ..._dev/mem 源码实现
文章浏览阅读7.1k次,点赞2次,收藏19次。vxe-table,一个小众但功能齐全并支持excel操作的vue表格组件_vxe-table
文章浏览阅读62次。参考:http://www.ruanyifeng.com/blog/2016/01/babel.htmlBabelBabel是一个广泛使用的转码器,可以将ES6代码转为ES5代码,从而在现有环境执行// 转码前input.map(item => item + 1);// 转码后input.map(function (item) { return item..._让开发环境支持bable
文章浏览阅读2.8k次,点赞6次,收藏29次。摘要:FPGA视频处理FIFO的典型应用,视频输入FIFO的作用,视频输出FIFO的作用,视频数据跨时钟域FIFO,视频缩放FIFO的作用_fpga 频分复用 视频
文章浏览阅读575次。【代码】R语言:设置工作路径为当前文件存储路径。_r语言设置工作目录到目标文件夹
文章浏览阅读452次。格式:background: linear-gradient(direction, color-stop1, color-stop2, ...);<linear-gradient> = linear-gradient([ [ <angle> | to <side-or-corner>] ,]? &l..._background线性渐变
文章浏览阅读1k次,点赞26次,收藏8次。第十三届蓝桥杯青少年组python编程省赛真题一、题目要求(注:input()输入函数的括号中不允许添加任何信息)1、编程实现给定一个正整数N,输出正整数N中各数位最大的那个数字。例如:N=132,则输出3。2、输入输出输入描述:只有一行,输入一个正整数N输出描述:只有一行,输出正整数N中各数位最大的那个数字输入样例:
文章浏览阅读2.2k次。一个网络协议主要由以下三个要素组成:1.语法数据与控制信息的结构或格式,包括数据的组织方式、编码方式、信号电平的表示方式等。2.语义即需要发出何种控制信息,完成何种动作,以及做出何种应答,以实现数据交换的协调和差错处理。3.时序即事件实现顺序的详细说明,以实现速率匹配和排序。不完整理解:语法表示长什么样,语义表示能干什么,时序表示排序。转载于:https://blog.51cto.com/98..._网络协议三要素csdn
文章浏览阅读153次。主要的思想,将所有的系统都可以看作两部分,真正的数据log系统和各种各样的query engine所有的一致性由log系统来保证,其他各种query engine不需要考虑一致性,安全性,只需要不停的从log系统来同步数据,如果数据丢失或crash可以从log系统replay来恢复可以看出kafka系统在linkedin中的重要地位,不光是d..._the log: what every software engineer should know about real-time data's uni
文章浏览阅读746次。伟大是熬出来的 目录 前言 引言 时间熬成伟大:领导者要像狼一样坚忍 第一章 内圣外王——领导者的心态修炼 1. 天纵英才的自信心 2. 上天揽月的企图心 3. 誓不回头的决心 4. 宠辱不惊的平常心 5. 换位思考的同理心 6. 激情四射的热心 第二章 日清日高——领导者的高效能修炼 7. 积极主动,想到做到 8. 合理掌控自己的时间和生命 9. 制定目标,马..._当狼拖着受伤的右腿逃生时,右腿会成为前进的阻碍,它会毫不犹豫撕咬断自己的腿, 以
文章浏览阅读285次。在当今的大数据时代,人们对高速度和高带宽的需求越来越大,迫切希望有一种新型产品来作为高性能计算和数据中心的主要传输媒质,所以有源光缆(AOC)在这种环境下诞生了。有源光缆究竟是什么呢?应用在哪些领域,有什么优势呢?易天将为您解答!有源光缆(Active Optical Cables,简称AOC)是两端装有光收发器件的光纤线缆,主要构成部件分为光路和电路两部分。作为一种高性能计..._aoc 光缆
文章浏览阅读2.2k次。在“桌面”上按快捷键“Ctrl+R”,调出“运行”窗口。接着,在“打开”后的输入框中输入“Gpedit.msc”。并按“确定”按钮。如下图 找到“用户配置”下的“Windows设置”下的“Internet Explorer 维护”的“连接”,双击选择“自动浏览器配置”。如下图 选择“自动启动配置”,并在下面的“自动代理URL”中填写相应的PAC文件地址。如下..._設置proxy腳本