技术标签: # 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
在多媒体通信领域,MRCP(Media Resource Control Protocol)协议被广泛用于控制语音识别和合成等媒体资源。UniMRCP是一个开源的MRCP实现,提供了客户端和服务端的库。UmcFramework是一个基于UniMRCP客户端库的示例应用程序框架,它帮助开发者快速集成和测试MRCP客户端功能。本文将详细介绍如何使用UmcFramework和unimrcpclient.xml配置文件连接到多个SIP设置,以及如何用C代码进行示例说明。
文章浏览阅读3k次。报错:java.net.ProtocolException: Server redirected too many times (20)1.没有检查到cookie,一直循环重定向。解决:CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));URL url = new URL(url); ..._java.net.protocolexception: server redirected too many times (20)
文章浏览阅读4.1k次。问题这是部分报错信息2019-07-11 14:03:34.283 WARN [restartedMain][DirectJDKLog.java:175] - Failed to scan [file:/D:/repo/org/apache/derby/derby/10.14.2.0/derbyLocale_ja_JP.jar] from classloader hierarchyjava...._failed to scan from classloader hierarchy
文章浏览阅读2.8k次,点赞3次,收藏7次。在MATLAB中,ones函数用于创建一个指定大小的由1组成的矩阵或数组。_matlab中ones函数
文章浏览阅读3.9w次,点赞2次,收藏9次。 在使用电脑办公过程中,安装应用程序时难免遇到无法安装或者无法正常启动的问题,这对我们使用电脑带来了诸多不便。那遇到应用程序无法正常启动的问题要如何解决呢?相信大家肯定都是十分疑问的,每次都是只能忍痛重新安装软件。今天,小编就和大家探讨下应用程序无法正常启动的解决方法,帮助大家排忧解难。0xc000007b电脑图解1 第一种方案:SFC检查系统完整性来尝试修复丢失文件 1、打开电脑搜索输入cmd.exe,选择以管理员身份运行,跳出提示框时选择继续。0xc000007b电脑图解2_photoshop应用程序无法正常启动0xc000007b。请单击“确认”关闭应用程序。
文章浏览阅读396次。1、概念 REDO LOG是Oracle为确保已经提交的事务不会丢失而建立的一个机制。实际上REDO LOG的存在是为两种场景准备的:实例恢复(INSTANCE RECOVERY);介质恢复(MEDIA RECOVERY)。 实例恢复的目的是在数据库发生故障时,确保BUFFER CACHE中的数据不会丢失,不会造成数据库的..._oracle 实例恢复和介质恢复
文章浏览阅读418次。概述说明CAS内置了密码找回和密码修改的功能; 密码找回功能是,系统会吧密码重置的连接通过邮件或短信方式发送给用户,用户点击链接后就可以重置密码,cas还支持预留密码重置的问题,只有回答对了,才可以重置密码;系统可配置密码重置后,是否自动登录; 密码修改功能是,用户登录后输入新密码即可完成密码修改。安装步骤`1. 首先,搭建好cas sso server您需要按..._修改cas默认用户密码
文章浏览阅读141次。之前几章演示的熔断,降级 都是 RestTemplate + Ribbon 和RestTemplate + Hystrix ,但是在实际开发并不是这样,实际开发中都是 Feign 远程接口调用。Feign + Hystrix 演示: eruka(略)order 服务工程: pom.xml<?xml version="1.0" encoding="U..._this is order 服务工程
文章浏览阅读3.4k次,点赞35次,收藏43次。学习率是影响目标检测精度和速度的重要因素之一。合适的学习率调度策略可以加速模型的收敛和提高模型的精度。在YOLOv7算法中,可以使用基于余弦函数的学习率调度策略(Cosine Annealing Learning Rate Schedule)来调整学习率。
文章浏览阅读4k次,点赞4次,收藏9次。 linux中进程退出函数:exit()和_exit()的区别(1)_exit()执行后立即返回给内核,而exit()要先执行一些清除操作,然后将控制权交给内核。(2)调用_exit函数时,其会关闭进程所有的文件描述符,清理内存以及其他一些内核清理函数,但不会刷新流(stdin, stdout, stderr ...). exit函数是在_exit..._linux结束进程可以用哪些函数,它们之间有何区别?
文章浏览阅读134次。select 5000/10000.0 --想变成0.5select 5500/10000.0 --想变成0.55select 5550/10000.0 --想变成0.555select 5555/10000.0 --想变成0.5555其结果分别为:0.5000000 0.5500000 0.5550000 0.5555000一、如果想去掉数字5后面多余的0 ,需要转化一下:selec..._sql server 去小数 0
文章浏览阅读3.1k次。例一:import { Injectable } from '@angular/core';import { Observable } from 'rxjs';import { User } from "./model/User";import { map } from 'rxjs/operators';import { Http, Response, Headers, RequestOp..._angular6,requestoptions改成了什么