技术标签: # 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
文章浏览阅读3k次,点赞3次,收藏13次。root@server ~]# vim /etc/named.rfc1912.zones #添加如下内容,也可直接更改模板。[root@server ~]# vim /etc/named.conf #打开主配置文件,将如下两处地方修改为。注意:ip地址必须反向书写,这里文件名需要和反向解析数据文件名相同。新建或者拷贝一份进行修改。nslookup命令。_dns反向解析
文章浏览阅读2.5w次,点赞16次,收藏103次。这个函数TIM_SetCompare1,这个函数有四个,分别是TIM_SetCompare1,TIM_SetCompare2,TIM_SetCompare3,TIM_SetCompare4。位于CH1那一行的GPIO口使用TIM_SetCompare1这个函数,位于CH2那一行的GPIO口使用TIM_SetCompare2这个函数。使用stm32f103的除了tim6和tim7没有PWM..._tim_setcompare1
文章浏览阅读950次,点赞33次,收藏19次。多线程_进程和线程,并发与并行,线程优先级,守护线程,实现线程的四种方式,线程周期;线程同步,线程中的锁,Lock类,死锁,生产者和消费者案例
文章浏览阅读2.9k次。ifort 编译器的安装ifort 编译器可以在 intel 官网上下载。打开https://software.intel.com/content/www/us/en/develop/tools/oneapi/components/fortran-compiler.html#gs.7iqrsm点击网页中下方处的 Download, 选择 Intel Fortran Compiler Classic and Intel Fortran Compiler(Beta) 下方对应的版本。我选择的是 l_在linux系统的用户目录下安装ifort和mkl库并配置
文章浏览阅读689次,点赞7次,收藏8次。些项目时需要一个生成图片的方法,我在网上找到比较方便且适合我去设置一些样式的生成方式之一就是使用Freemarker,在对应位置上先写好一个html格式的ftl文件,在对应位置用${参数名}填写上。还记得当时为了解决图片大小设置不上,搜索了好久资料,不记得是在哪看到的需要在里面使用width与height直接设置,而我当时用style去设置,怎么都不对。找不到,自己测试链接,准备将所有含有中文的图片链接复制一份,在服务器上存储一份不带中文的文件。突然发现就算无中文,有的链接也是打不开的。_ftl格式pdf的样式调整
文章浏览阅读1.5k次,点赞6次,收藏12次。拉取librealsense。_opt/ros/noetic/lib/nodelet/nodelet: symbol lookup error: /home/admin07/reals
文章浏览阅读3.4k次,点赞3次,收藏29次。一.单选题二.填空题三.判断题一.单选题静态链接是在( )进行的。A、编译某段程序时B、装入某段程序时C、紧凑时D、装入程序之前Pentium处理器(32位)最大可寻址的虚拟存储器地址空间为( )。A、由内存的容量而定B、4GC、2GD、1G分页系统中,主存分配的单位是( )。A、字节B、物理块C、作业D、段在段页式存储管理中,当执行一段程序时,至少访问()次内存。A、1B、2C、3D、4在分段管理中,( )。A、以段为单位分配,每._系统抖动现象的发生由什么引起的
文章浏览阅读2.4k次。在实际的工作生产中,零件的加工制造一般都需要二维工程图来辅助设计。UG NX 的工程图主要是为了满足二维出图需要。在绘制工程图时,需要先确定所绘制图形要表达的内容,然后根据需要并按照视图的选择原则,绘制工程图的主视图、其他视图以及某些特殊视图,最后标注图形的尺寸、技术说明等信息,即可完成工程图的绘制。1.视图选择原则工程图合理的表达方案要综合运用各种表达方法,清晰完整地表达出零件的结构形状,并便于看图。确定工程图表达方案的一般步骤如下:口分析零件结构形状由于零件的结构形状以及加工位置或工作位置的不._ug-nx工程图
文章浏览阅读920次,点赞29次,收藏18次。原文《智能制造数字化工厂智慧供应链大数据解决方案》PPT格式主要从智能制造数字化工厂智慧供应链大数据解决方案框架图、销量预测+S&OP大数据解决方案、计划统筹大数据解决方案、订单履约大数据解决方案、库存周转大数据解决方案、采购及供应商管理大数据模块、智慧工厂大数据解决方案、设备管理大数据解决方案、质量管理大数据解决方案、仓储物流与网络优化大数据解决方案、供应链决策分析大数据解决方案进行建设。适用于售前项目汇报、项目规划、领导汇报。
文章浏览阅读2w次,点赞38次,收藏102次。在服务器端,socket()返回的套接字用于监听(listen)和接受(accept)客户端的连接请求。这个套接字不能用于与客户端之间发送和接收数据。 accept()接受一个客户端的连接请求,并返回一个新的套接字。所谓“新的”就是说这个套接字与socket()返回的用于监听和接受客户端的连接请求的套接字不是同一个套接字。与本次接受的客户端的通信是通过在这个新的套接字上发送和接收数_当在函数 'main' 中调用 'open_socket_accept'时.line: 8. connection request fa
文章浏览阅读4.3k次。对象销毁对象销毁的标准语法Close和Stop何时销毁对象销毁对象时清除字段对象销毁的标准语法Framework在销毁对象的逻辑方面遵循一套规则,这些规则并不限用于.NET Framework或C#语言;这些规则的目的是定义一套便于使用的协议。这些协议如下:一旦销毁,对象不可恢复。对象不能被再次激活,调用对象的方法或者属性抛出ObjectDisposedException异常重复地调用对象的Disposal方法会导致错误如果一个可销毁对象x 包含或包装或处理另外一个可销毁对象y,那么x的Disp_c# 销毁对象及其所有引用
文章浏览阅读1.1w次。这是记录,在中项、高项过程中的错题笔记;https://www.zenwu.site/post/2b6d.html1. 信息系统的规划工具在制订计划时,可以利用PERT图和甘特图;访谈时,可以应用各种调查表和调查提纲;在确定各部门、各层管理人员的需求,梳理流程时,可以采用会谈和正式会议的方法。为把企业组织结构与企业过程联系起来,说明每个过程与组织的联系,指出过程决策人,可以采用建立过程/组织(Process/Organization,P/O)矩阵的方法。例如,一个简单的P/O矩阵示例,其中._大型设备可靠性测试可否拆解为几个部分进行测试