- 浏览: 229315 次
- 性别:
- 来自: 武汉
-
文章分类
- 全部博客 (148)
- ext (3)
- flex (22)
- css (4)
- struts (5)
- test (1)
- spring (8)
- jquery (4)
- eclipse plugin (4)
- ant (2)
- JSP (7)
- java (15)
- blaze (1)
- html (4)
- jquery js (9)
- 测试 testDirector 设置 windows7 ie9 (1)
- flex twaver (1)
- flex twaver (1)
- 网管系统 (1)
- 数据库 mysql (1)
- HTML5 (2)
- 测试 (1)
- js (6)
- 软件方法 软件管理 (1)
- jquery jqgrid (3)
- flex xml (1)
- jquery validation (1)
- 网址 (3)
- spring security (2)
- 学习列表 (1)
- java 建站 (1)
- firefox 插件 (1)
- 非技术 (1)
- 百度地图 API (2)
- ajax (1)
- 3D (1)
- 矩阵 (1)
- WAP (6)
- 百度地图 (1)
- 模拟器 (1)
- eclipse (2)
- tomcat (2)
- eclipse j2ee (1)
- 天气预报 (1)
- API (2)
- ruby (1)
- Rails (2)
- jquerymobile (1)
- 股票 (1)
- goagent (0)
- 代理 (0)
- GitHub (1)
- 代码托管 (1)
- 安装 (1)
- maven (1)
- MVC (1)
- 面试题 (1)
- java 排序 算法 (1)
- java 算法 堆排序 (1)
- AS3 (1)
- html5 css3 (1)
- j2ee (1)
- 数据库 oracle (1)
- 显卡 (1)
- json (1)
- 笔记本选购 (0)
- 华硕 笔记本 USB (1)
- word (1)
- 试音音源 (1)
- junit (1)
- Quartz (1)
- 模拟 (0)
- nodejs (0)
- 12306 (1)
- 内存调整 (1)
- 正则表达式 (2)
- 金融 (1)
- easyui (1)
- oracle (0)
- sql (0)
- 设计模式 (1)
- 报表 (1)
- linux (1)
- windows (1)
- ftp (1)
- JWTs (0)
- JBPM4 (0)
- 对账 (1)
- 银企直连 (1)
- 商户 (1)
- 支付宝 (1)
- ljk (0)
- webservice (1)
- git (1)
- js vue (1)
最新评论
-
喵喵大神:
免费股票数据查询API:https://www.juhe.cn ...
股票数据接口API -
surfingll:
谢谢啦,有用
ajaxAnywhere局部刷新(一) -
631134230:
怎么设置饼图 不要爆炸式进场动画效果
amcharts js 制作柱状统计图和饼图 -
musicbox95351:
可以自己用ajax请求数据后,在回调方法里面去执行AmAngu ...
amcharts js 制作柱状统计图和饼图 -
pandatyut:
大神,请问amchart的仪表盘AmAngularGauge能 ...
amcharts js 制作柱状统计图和饼图
先备份一下。
http://elf8848.iteye.com/blog/875830
以下是《Spring Framework Reference Documentation》3.2.1学习笔记
目标 在mvc-ajax示例代码的基础上完成订单提交部分的异步验证功能
已完成在mvc-basic示例基础上实现完整的web侧订单(包括子项目)录入,支持国际化
web.xml中配置org.springframework.web.servlet.DispatcherServlet。也可以以编码方式配置。
org.springframework.web.servlet包中的DispatcherServlet.properties描述了DispatcherServlet维护的一组对特殊bean的默认实现。
WebAplicationContext在ServletContext中
通过RequestContextUtils的静态方法可以获得WebAplicationContext
实现Controllers
如果URL模板变量名和方法参数变量名相同,可以省略@PathVariable注解中的变量名字符串。
一个方法可以有任意多个@PathVariable注解。
@PathVariable注解用于Map<String, String>类型的参数时,URL模板变量将自动映射到map参数中。
URL模板可以由类级别和方法级别的@RequestMapping注解组合而成:如
findPet() 方法可以这样访问 /owners/42/pets/21
@RequestMapping注解支持正则表达式。语法为:{varName:regex}。用法如:
访问路径可能为:"/spring-web/spring-web-3.0.5.jar"
配置<context:component-scan base-package="*.web"/>来让spring自动发现添加了@Controller注解的类。
可以对Controller进行范围缩小,如:
@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")
中的consumes="application/json"条件
@Controller
@RequestMapping(value = "/pets/{petId}", method =
RequestMethod.GET, produces="application/json")
@ResponseBody //指定用来生成response
public Pet getPet(@PathVariable String petId, Model model) {
// implementation omitted
}
@RequestMapping(value = "/pets/{petId}", method =
RequestMethod.GET, params="myParam=myValue") //这里的params参数
//该参数内容可以是"myParam", "!myParam", or "myParam=myValue".
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model
model) {
// implementation omitted
}
使用@RequestParam参数绑定request参数到方法参数。如:
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
Pet pet = this.clinic.loadPet(petId);
model.addAttribute("pet", pet);
return "petForm";
}
@RequestMapping(value = "/pets", method = RequestMethod.GET, headers="myHeader=myValue")
//这里通过对请求header中的属性和值进行过滤
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model
model) {
// implementation omitted
}
定义@RequestMapping处理方法
@RequestMapping处理方法支持的一批参数,见“Supported method argument types”部分
Errors 或者 BindingResult参数必须紧跟@ModelAttribute注解的参数,
下面的BindingResult result参数应该移到pet参数后面。
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet,
Model model, BindingResult result) { … }
@ModelAttribute参数可能会有多个,spring会为每一个这种对象生成一个 BindingResult
使用@RequestBody映射request body
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}
使用@ResponseBody映射response body
@RequestMapping(value = "/something", method = RequestMethod.PUT)
@ResponseBody
public String helloWorld() {
return "Hello World";
}
使用 HttpEntity<?>
HttpEntity被允许访问request 和response body
@RequestMapping("/something")
public ResponseEntity<String> handle(HttpEntity<byte[]> requestEntity) throws
UnsupportedEncodingException {
String requestHeader = requestEntity.getHeaders().getFirst("MyRequestHeader"));
byte[] requestBody = requestEntity.getBody();
// do something with request header and body
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
}
在方法上使用@ModelAttribute
// Add one attribute
// The return value of the method is added to the model under the name "account"
// You can customize the name via @ModelAttribute("myAccount")
@ModelAttribute
public Account addAccount(@RequestParam String number) {
return accountManager.findAccount(number);
}
// Add multiple attributes
@ModelAttribute
public void populateModel(@RequestParam String number, Model model) {
model.addAttribute(accountManager.findAccount(number));
// add more ...
}
方法参数中使用@ModelAttribute
@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) {
}
@RequestMapping(value="/accounts/{account}", method = RequestMethod.PUT)
public String save(@ModelAttribute("account") Account account) {
}
JSR-303 @Valid annotation验证
@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@Valid @ModelAttribute("pet") Pet pet, BindingResult result) {
if (result.hasErrors()) {
return "petForm";
}
// ...
}
使用@SessionAttributes保存model属性到session
@Controller
@RequestMapping("/editPet.do")
@SessionAttributes("pet")
public class EditPetForm {
// ...
}
如果使用controller接口,则@RequestMapping and @SessionAttributes这些注解应该放在接口中。
使用@CookieValue映射cookie属性
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) {
//...
}
使用@RequestHeader 映射request header属性
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
@RequestHeader("Keep-Alive") long keepAlive) {
//...
}
自定义参数类型转换见“Customizing WebDataBinder initialization”章节或者7.6节
使用@InitBinder自定义数据绑定
@Controller
public class MyFormController {
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
// ...
}
异步请求处理。返回Callable
@RequestMapping(method=RequestMethod.POST)
public Callable<String> processUpload(final MultipartFile file) {
return new Callable<String>() {
public Object call() throws Exception {
// ...
return "someView";
}
};
}
用来响应JMS等外部事件的请求
@RequestMapping("/quotes")
@ResponseBody
public DeferredResult<String> quotes() {
DeferredResult<String> deferredResult = new DeferredResult<String>();
// Save the deferredResult in in-memory queue ...
return deferredResult;
}
// In some other thread...
deferredResult.setResult(data);
使用HandlerInterceptor拦截请求,实现自定义的对请求的拦截。
可以继承HandlerInterceptorAdapter来简化自定义实现。
见Intercepting requests with a HandlerInterceptor章节。
对View的处理
spring提供的View处理器
AbstractCachingViewResolver 抽象类。是其它具体处理器的父类。扩展该处理器可提供缓存。
XmlViewResolver
ResourceBundleViewResolver 可以用于配置处理多种视图处理方式。比如配置某些url使用某种处理器。
UrlBasedViewResolver 可以处理jsp视图。是AbstractCachingViewResolver 的子类。
InternalResourceViewResolver 可以处理Servlets and JSPs。是UrlBasedViewResolver 的子类。
VelocityViewResolver /FreeMarkerViewResolver
ContentNegotiatingViewResolver
视图处理器链
需要注意的是InternalResourceViewResolver处理器应该配置在最后,
或者说要让InternalResourceViewResolver处理器做最后的尝试。
因为InternalResourceViewResolver始终会返回一个视图,一旦返回,其它视图处理器就不会再处理了。
视图重定向
一般情况下,一个controller接受到一个请求后会返回一个逻辑视图名称然后交由特定的视图处理器处理。
但是有些情况需要在视图被渲染以前重定向到客户端。
比如,防止用户刷新页面时造成表单重提交。
再比如,将post请求交给其它controller继续处理。(这部分需要实践加深理解了)
URL模板变量在重定向时是可以直接使用的。
使用flash属性
建议只在重定向时使用。一般的请求也不需要使用该属性。
FlashMap用来保存flash属性。可通过RequestContextUtils的静态方法获取FlashMap。
使用locaels
通过RequestContext.getLocale()可以获取locale处理器处理后的locale
以下是spring提供的国际化处理器:
AcceptHeaderLocaleResolver
该处理器会检查请求中的“accept-language” 头信息。通常为操作系统的locale信息。
CookieLocaleResolver
该处理器会检查请求中的“accept-language” 头信息。
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="clientlanguage"/>
<!-- in seconds. If set to -1, the cookie is not persisted (deleted when browser shuts
down) -->
<property name="cookieMaxAge" value="100000">
</bean>
SessionLocaleResolver
从locale获取locale
LocaleChangeInterceptor
配置拦截器来处理国际化(也是spring官方示例中使用的配置)
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
教程中的示例
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="siteLanguage"/><!-- siteLanguage为请求中的参数名称 -->
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
<property name="mappings">
<value>/**/*.view=someController</value>
</property>
</bean>
spring对文件上传的支持
基于commons-fileupload.jar的文件上传
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
导入commons-fileupload.jar
基于Servlet 3.0的文件上传
在web.xml中配置"multipart-config" 部分
配置
<bean id="multipartResolver"
class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
页面部分
<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="/form" enctype="multipart/form-data">
<input type="text" name="name"/>
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
controller部分
@Controller
public class FileUploadController {
@RequestMapping(value = "/form", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:uploadSuccess";
} else {
return "redirect:uploadFailure";
}
}
}
如果是使用Servlet3.0可以用javax.servlet.http.Part
@Controller
public class FileUploadController {
@RequestMapping(value = "/form", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name,
@RequestParam("file") Part file) {
InputStream inputStream = file.getInputStream();
// store bytes from uploaded file somewhere
return "redirect:uploadSuccess";
}
}
Convention配置
没有struts2的零配置好用
配置默认首页
<!-- web.xml中配置首页 这种方式在处理国际化时会有些问题-->
<welcome-file-list>
<welcome-file>/WEB-INF/views/account/welcome.jsp</welcome-file>
</welcome-file-list>
DispatcherServlet的配置文件中配置首页
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:view-controller path="/" view-name="account/welcome"/>
静态资源的配置
表示通过http://.../resources/**路径访问时
将可以在项目的根路径或者jar包中的META-INF/public-web-resources/路径下找到资源文件。
在比如
可以将js文件放在项目的/WEB-INF/js/目录下。然后再jsp文件中访问
http://elf8848.iteye.com/blog/875830
以下是《Spring Framework Reference Documentation》3.2.1学习笔记
目标 在mvc-ajax示例代码的基础上完成订单提交部分的异步验证功能
已完成在mvc-basic示例基础上实现完整的web侧订单(包括子项目)录入,支持国际化
web.xml中配置org.springframework.web.servlet.DispatcherServlet。也可以以编码方式配置。
org.springframework.web.servlet包中的DispatcherServlet.properties描述了DispatcherServlet维护的一组对特殊bean的默认实现。
WebAplicationContext在ServletContext中
通过RequestContextUtils的静态方法可以获得WebAplicationContext
实现Controllers
引用
@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable("ownerId") String theOwner, Model model) {
// implementation omitted
}
Or if the URI template variable name matches the method argument name you can omit that
detail.
public String findOwner(@PathVariable("ownerId") String theOwner, Model model) {
// implementation omitted
}
Or if the URI template variable name matches the method argument name you can omit that
detail.
如果URL模板变量名和方法参数变量名相同,可以省略@PathVariable注解中的变量名字符串。
引用
A method can have any number of @PathVariable annotations
一个方法可以有任意多个@PathVariable注解。
引用
When a @PathVariable annotation is used on a Map<String, String> argument, the map is
populated with all URI template variables.
populated with all URI template variables.
@PathVariable注解用于Map<String, String>类型的参数时,URL模板变量将自动映射到map参数中。
引用
A URI template can be assembled from type and path level @RequestMapping annotations. As a result
the findPet() method can be invoked with a URL such as /owners/42/pets/21
the findPet() method can be invoked with a URL such as /owners/42/pets/21
URL模板可以由类级别和方法级别的@RequestMapping注解组合而成:如
@Controller @RequestMapping("/owners/{ownerId}") public class RelativePathUriTemplateController { @RequestMapping("/pets/{petId}") public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) { // implementation omitted } }
findPet() 方法可以这样访问 /owners/42/pets/21
@RequestMapping注解支持正则表达式。语法为:{varName:regex}。用法如:
@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}") public void handle(@PathVariable String version, @PathVariable String extension) { // ... } }
访问路径可能为:"/spring-web/spring-web-3.0.5.jar"
配置<context:component-scan base-package="*.web"/>来让spring自动发现添加了@Controller注解的类。
可以对Controller进行范围缩小,如:
@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")
中的consumes="application/json"条件
@Controller
@RequestMapping(value = "/pets/{petId}", method =
RequestMethod.GET, produces="application/json")
@ResponseBody //指定用来生成response
public Pet getPet(@PathVariable String petId, Model model) {
// implementation omitted
}
@RequestMapping(value = "/pets/{petId}", method =
RequestMethod.GET, params="myParam=myValue") //这里的params参数
//该参数内容可以是"myParam", "!myParam", or "myParam=myValue".
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model
model) {
// implementation omitted
}
使用@RequestParam参数绑定request参数到方法参数。如:
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
Pet pet = this.clinic.loadPet(petId);
model.addAttribute("pet", pet);
return "petForm";
}
@RequestMapping(value = "/pets", method = RequestMethod.GET, headers="myHeader=myValue")
//这里通过对请求header中的属性和值进行过滤
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model
model) {
// implementation omitted
}
定义@RequestMapping处理方法
@RequestMapping处理方法支持的一批参数,见“Supported method argument types”部分
Errors 或者 BindingResult参数必须紧跟@ModelAttribute注解的参数,
下面的BindingResult result参数应该移到pet参数后面。
@RequestMapping(method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet,
Model model, BindingResult result) { … }
@ModelAttribute参数可能会有多个,spring会为每一个这种对象生成一个 BindingResult
使用@RequestBody映射request body
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}
使用@ResponseBody映射response body
@RequestMapping(value = "/something", method = RequestMethod.PUT)
@ResponseBody
public String helloWorld() {
return "Hello World";
}
使用 HttpEntity<?>
HttpEntity被允许访问request 和response body
@RequestMapping("/something")
public ResponseEntity<String> handle(HttpEntity<byte[]> requestEntity) throws
UnsupportedEncodingException {
String requestHeader = requestEntity.getHeaders().getFirst("MyRequestHeader"));
byte[] requestBody = requestEntity.getBody();
// do something with request header and body
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
}
在方法上使用@ModelAttribute
// Add one attribute
// The return value of the method is added to the model under the name "account"
// You can customize the name via @ModelAttribute("myAccount")
@ModelAttribute
public Account addAccount(@RequestParam String number) {
return accountManager.findAccount(number);
}
// Add multiple attributes
@ModelAttribute
public void populateModel(@RequestParam String number, Model model) {
model.addAttribute(accountManager.findAccount(number));
// add more ...
}
方法参数中使用@ModelAttribute
@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) {
}
@RequestMapping(value="/accounts/{account}", method = RequestMethod.PUT)
public String save(@ModelAttribute("account") Account account) {
}
JSR-303 @Valid annotation验证
@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@Valid @ModelAttribute("pet") Pet pet, BindingResult result) {
if (result.hasErrors()) {
return "petForm";
}
// ...
}
使用@SessionAttributes保存model属性到session
@Controller
@RequestMapping("/editPet.do")
@SessionAttributes("pet")
public class EditPetForm {
// ...
}
如果使用controller接口,则@RequestMapping and @SessionAttributes这些注解应该放在接口中。
使用@CookieValue映射cookie属性
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) {
//...
}
使用@RequestHeader 映射request header属性
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
@RequestHeader("Keep-Alive") long keepAlive) {
//...
}
自定义参数类型转换见“Customizing WebDataBinder initialization”章节或者7.6节
使用@InitBinder自定义数据绑定
@Controller
public class MyFormController {
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
// ...
}
异步请求处理。返回Callable
@RequestMapping(method=RequestMethod.POST)
public Callable<String> processUpload(final MultipartFile file) {
return new Callable<String>() {
public Object call() throws Exception {
// ...
return "someView";
}
};
}
用来响应JMS等外部事件的请求
@RequestMapping("/quotes")
@ResponseBody
public DeferredResult<String> quotes() {
DeferredResult<String> deferredResult = new DeferredResult<String>();
// Save the deferredResult in in-memory queue ...
return deferredResult;
}
// In some other thread...
deferredResult.setResult(data);
使用HandlerInterceptor拦截请求,实现自定义的对请求的拦截。
可以继承HandlerInterceptorAdapter来简化自定义实现。
见Intercepting requests with a HandlerInterceptor章节。
对View的处理
spring提供的View处理器
AbstractCachingViewResolver 抽象类。是其它具体处理器的父类。扩展该处理器可提供缓存。
XmlViewResolver
ResourceBundleViewResolver 可以用于配置处理多种视图处理方式。比如配置某些url使用某种处理器。
UrlBasedViewResolver 可以处理jsp视图。是AbstractCachingViewResolver 的子类。
InternalResourceViewResolver 可以处理Servlets and JSPs。是UrlBasedViewResolver 的子类。
VelocityViewResolver /FreeMarkerViewResolver
ContentNegotiatingViewResolver
视图处理器链
需要注意的是InternalResourceViewResolver处理器应该配置在最后,
或者说要让InternalResourceViewResolver处理器做最后的尝试。
因为InternalResourceViewResolver始终会返回一个视图,一旦返回,其它视图处理器就不会再处理了。
视图重定向
一般情况下,一个controller接受到一个请求后会返回一个逻辑视图名称然后交由特定的视图处理器处理。
但是有些情况需要在视图被渲染以前重定向到客户端。
比如,防止用户刷新页面时造成表单重提交。
再比如,将post请求交给其它controller继续处理。(这部分需要实践加深理解了)
URL模板变量在重定向时是可以直接使用的。
使用flash属性
建议只在重定向时使用。一般的请求也不需要使用该属性。
FlashMap用来保存flash属性。可通过RequestContextUtils的静态方法获取FlashMap。
使用locaels
通过RequestContext.getLocale()可以获取locale处理器处理后的locale
以下是spring提供的国际化处理器:
AcceptHeaderLocaleResolver
该处理器会检查请求中的“accept-language” 头信息。通常为操作系统的locale信息。
CookieLocaleResolver
该处理器会检查请求中的“accept-language” 头信息。
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieName" value="clientlanguage"/>
<!-- in seconds. If set to -1, the cookie is not persisted (deleted when browser shuts
down) -->
<property name="cookieMaxAge" value="100000">
</bean>
SessionLocaleResolver
从locale获取locale
LocaleChangeInterceptor
配置拦截器来处理国际化(也是spring官方示例中使用的配置)
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
教程中的示例
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="siteLanguage"/><!-- siteLanguage为请求中的参数名称 -->
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
<property name="mappings">
<value>/**/*.view=someController</value>
</property>
</bean>
spring对文件上传的支持
基于commons-fileupload.jar的文件上传
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
导入commons-fileupload.jar
基于Servlet 3.0的文件上传
在web.xml中配置"multipart-config" 部分
配置
<bean id="multipartResolver"
class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
页面部分
<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="/form" enctype="multipart/form-data">
<input type="text" name="name"/>
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
controller部分
@Controller
public class FileUploadController {
@RequestMapping(value = "/form", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
// store the bytes somewhere
return "redirect:uploadSuccess";
} else {
return "redirect:uploadFailure";
}
}
}
如果是使用Servlet3.0可以用javax.servlet.http.Part
@Controller
public class FileUploadController {
@RequestMapping(value = "/form", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name,
@RequestParam("file") Part file) {
InputStream inputStream = file.getInputStream();
// store bytes from uploaded file somewhere
return "redirect:uploadSuccess";
}
}
Convention配置
没有struts2的零配置好用
配置默认首页
<!-- web.xml中配置首页 这种方式在处理国际化时会有些问题-->
<welcome-file-list>
<welcome-file>/WEB-INF/views/account/welcome.jsp</welcome-file>
</welcome-file-list>
DispatcherServlet的配置文件中配置首页
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<mvc:view-controller path="/" view-name="account/welcome"/>
静态资源的配置
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/public-web-resources/"/>
表示通过http://.../resources/**路径访问时
将可以在项目的根路径或者jar包中的META-INF/public-web-resources/路径下找到资源文件。
在比如
<mvc:resources mapping="/resources/**" location="/resources/,/WEB-INF/js/" />
可以将js文件放在项目的/WEB-INF/js/目录下。然后再jsp文件中访问
<script src="<c:url value="/resources/jquery.min-1.9.1.js" />"></script>
- spring-framework-reference.pdf (4.5 MB)
- 下载次数: 2
- springMVC示例包含一个模拟的包含子项目的订单提交支持国际化.rar (233.8 KB)
- 下载次数: 0
发表评论
-
Junit4测试spring 组件
2013-10-15 17:32 727很简单,但是还是自己备份一下。 package com.x ... -
spring相关资料
2013-03-07 14:24 691spring 3.x示例下载svn https://anons ... -
java spring 公共属性配置 备忘
2011-12-20 21:38 795<bean id="publicAttribu ... -
java spring 属性注入 备忘
2011-12-19 22:01 889<bean id="" class= ... -
spring 转一篇 bean的作用域
2011-12-15 22:05 751http://www.cnblogs.com/franklii ... -
flex spring blaze 整合相关
2011-10-20 16:51 1046http://bbs.airia.cn/FLEX/thread ... -
由spring管理properties文件的资源
2011-04-18 16:13 1307一种方式是将一批资源文件交由spring管理,在类中以@Val ...
相关推荐
Spring MVC 是一个基于Java的轻量级Web应用框架,它为开发者提供了模型-视图-控制器(MVC)架构,使开发人员能够更好地组织和分离应用程序的业务逻辑、数据处理和用户界面。Spring MVC是Spring框架的一个核心组件,...
Spring MVC 是一个强大的Java Web开发框架,它是Spring框架的一部分,专为构建高度可扩展和模块化的Web应用程序而设计。在2015年的版本中,Spring MVC 4已经相当成熟,提供了许多特性来简化开发流程并提高开发效率。...
这是一个基于Spring MVC、Mybatis和Spring框架实现的个人博客系统,涵盖了Web开发中的后端架构设计、数据库管理和前端展示等多个方面。以下将详细介绍这个系统的关键知识点: **1. Spring MVC** Spring MVC是Spring...
Spring MVC 是一个基于 Java 的轻量级 Web 开发框架,它是 Spring 框架的一个重要模块,主要用于构建 Web 应用程序的后端控制层。这个框架提供了模型-视图-控制器(MVC)设计模式的实现,简化了Java Web应用的开发...
Spring MVC 是一个基于 Java 的轻量级Web应用框架,它为构建模型-视图-控制器(MVC)架构的应用程序提供了强大的支持。在本压缩包中包含了一系列与Spring MVC相关的jar文件,这些文件是构建和运行Spring MVC项目所...
在本项目中,我们主要探讨的是如何利用Spring MVC和Spring Security框架构建一个基本的无数据库登录系统。Spring MVC是Spring框架的一部分,用于处理Web应用程序的请求-响应模型,而Spring Security则是一个强大的...
Spring MVC 和 MyBatis 是两个在Java Web 开发中广泛使用的框架。Spring MVC 作为Spring框架的一部分,负责处理HTTP请求并转发到相应的业务逻辑,而MyBatis则是一个轻量级的持久层框架,用于简化数据库操作。整合这...
Spring MVC 是一个基于Java的轻量级Web应用框架,它属于Spring框架的一部分,主要用于构建控制器层,实现模型-视图-控制器(Model-View-Controller)架构。在Spring MVC中,开发者可以方便地处理HTTP请求,进行数据...
Spring MVC是Spring框架的一个核心模块,专为构建Web应用程序而设计。它提供了模型-视图-控制器(MVC)架构,使开发者能够有效地分离业务逻辑、数据处理和用户界面。在"Spring MVC 4.2.3"版本中,我们看到了一系列的...
Spring MVC 是一个基于Java的轻量级Web应用框架,它是Spring框架的重要组成部分,主要用于构建Web应用程序的后端控制器。这个"Spring MVC 基础实例源码01"的资源很可能是为了帮助初学者理解Spring MVC的核心概念和...
**Spring MVC 4.2.4.RELEASE 中文文档** Spring MVC是Spring框架的一个核心组件,专注于构建Web应用程序。它提供了模型-视图-控制器(MVC)架构,帮助开发者组织和分离应用的业务逻辑、数据处理以及用户界面。...
Spring MVC是一种基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web框架,使用了IoC容器,支持RESTful风格的应用程序开发。Spring MVC通过分离模型(Model)、视图(View)和控制器(Controller)来简化Web开发...
Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)SSM源码Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)SSM源码Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)SSM源码Java EE企业级应用...
在IT行业中,Spring MVC 和 MyBatis 是两个非常重要的框架,它们分别负责Web应用程序的控制器层和数据访问层。Spring MVC 提供了模型-视图-控制器架构模式的实现,而MyBatis则是一个轻量级的SQL映射框架,用于简化...
《基于Spring、Spring MVC、MyBatis的Java客户关系管理系统详解》 在现代企业运营中,客户关系管理(CRM)系统扮演着至关重要的角色,它帮助企业有效地管理与客户的交互,提高销售效率,优化客户服务,从而增强企业...
Spring MVC是Spring框架的一个核心模块,专用于构建Web应用程序。这个"Spring MVC使用Demo"提供了实践操作,帮助开发者深入理解Spring MVC的开发环境配置、注解的使用以及工作原理。 首先,Spring MVC的设计模式...
Spring MVC是Spring框架的一个模块,主要用于构建Web应用程序,而Spring JDBC则是Spring提供的用于简化数据库操作的工具。 【描述】"用spring4.0.5版本开发的,spring mvc,spring jdbc,最简单的用户登录,注册和...