Learn springmvc--internationalization + upload + download detailed _java

Source: Internet
Author: User
Tags file upload getmessage i18n locale tomcat

A software, a product, are a little bit of development and perfect up, more and more functions, more and more performance, the user experience more and more good ... The improvement of each indicator requires a bit of something to do, like, your product big, with more people, not only Shanghai people use, Beijing people use, and Indians use, the French and so on, it can be said that the product has embarked on the international arena. When the Indian buddies enter the URL to access the product, the interface pop-up "welcome you, three elder brother", estimated that the man on the spot on the ring. And this time, internationalization came into being.

To do the internationalization of this dish, really not imagined so complex, but very simple, do not believe you see--

1. Inject Resourcebundlemessagesource

Add Beanresourcebundlmessagesource for internationalized processing in Springmvc.xml

<bean id= "Messagesource" class= "Org.springframework.context.support.ResourceBundleMessageSource" > 

  < Property Name= "basename" value= "i18n" ></property> 

</bean> 

The name in this property is the same as the attribute name in the injection class, where value determines the name of the later internationalized file, and it is i18n, and you will see its usage immediately.

2. Create internationalized Files

A total of three internationalized attribute files need to be created

i18n.properties-the default internationalization file

i18n_en_us.properties-International documents for use in English environment

i18n_zh_cn.properties-internationalization files for the Chinese environment

Note: This is why the name of the file is i18n beginning, because in the 1th springmvc.xml configuration file, the value of the configuration is i18n

The contents of the I18n.properties and i18n_en_us.properties files are the same, as follows

I18n.username=username

I18n.password=password 

I18n_zh_cn.properties

i18n.username=\u7528\u6237\u540d

i18n.password=\u5bc6\u7801 

3. New Page

Create a new two pages, one is i18n.jsp, display the user name, at the same time there is a jump to i18n2.jsp hyperlink, the other is i18n2.jsp, display password, there are jumps to i18n.jsp hyperlinks.

i18n.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "

  pageencoding=" UTF-8 "%> <%@ taglib prefix=" FMT "uri=" Http://java.sun.com/jsp/jstl/fmt

"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

 
 

i18n2.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "

  pageencoding=" UTF-8 "%> <%@ taglib prefix=" FMT "uri=" Http://java.sun.com/jsp/jstl/fmt

"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

 
 

At the same time, obviously we need to add a portal to the index.jsp and link to the i18n.jsp page, as follows

 
 

In order to be able to directly click on the link past, and do not need to handler processing and jump to the view of the routine, we need to add tags in the springmvc.xml

<mvc:view-controller path= "/i18n" view-name= "i18n"/> <mvc:view-controller path= "/i18n2" 

I18n2 "/> 

This will enable direct access to the i18n.jsp and i18n2.jsp pages directly in the address bar.

  

Small pits: If the encoding in i18n.jsp and i18n2.jsp adopts the default "Iso-8859-1", the page will appear garbled

When the code is changed to "UTF-8", it can be displayed correctly.

The above is the basic practice of internationalization of this dish, then if I also want to do together without direct access to i18n.jsp, but after handler processing i18n.jsp, or I want to do a not so troublesome also to switch the language of the international dishes, there is no possibility, of course, then see-

1. Add tags to i18n.jsp direct access before Springmvc.xml

<!-- 

<mvc:view-controller path= "/i18n" view-name= "i18n"/> 

--> 

2. Add a processing interface to the Hanlder processing class Springmvctest

@Autowired

private Resourcebundlemessagesource messagesource;

@RequestMapping ("/i18n") public

String testi18n (Locale Locale) {

  string val = Messagesource.getmessage (" I18n.username ", null, locale);

  System.out.println (val);

  return "i18n";

} 

Note that the internationalized processing class Resourcebundlemessagesource is injected and the Internationalized property value is obtained using its GetMessage method.

Start the Tomcat service to see

So if you're setting up your own language, you can display information in a different language environment.

1. Configure Sessionlocaleresolver and Localechangeinterceptor

<!--configuration Sessionlocaleresolver-->

<bean id= "Localeresolver" class= " Org.springframework.web.servlet.i18n.SessionLocaleResolver "></bean>

       

<!-- Configure Localechangeinterceptor-->

<mvc:interceptors>

  <bean class= " Org.springframework.web.servlet.i18n.LocaleChangeInterceptor "></bean>

</mvc:interceptors> 

The localechangeinterceptor here is primarily used to parse a request with locale information into a locale object and get a Localeresolver object

The sessionlocalresolver then converts the above Localresolver object into the session's properties and extracts the property, which is the locale object, back to the application.

2. Add a hyperlink in index.jsp

<a href= "I18N?LOCALE=ZH_CN" > Chinese </a>

<br><br>

<a href= "I18n?locale=en_us" > English </a> 

So we can see the results

Speaking of internationalization, let's say SPRINGMVC support for JSON.

In the traditional development process, our handler-controller layer usually follows a routine that needs to be shifted to a JSP view, but such scenarios do not meet all requirements, such as we often just need to return the data, not a JSP page. Then spring MVC3 's @responsebody and @responseentity support such a feature. Instead of directly pointing to a specific view, controller directly returns the data (here we talk about JSON data). Here is a simple example of upload and download.

1. File Upload

1.1 using jquery to implement AJAX requests in index.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  

The core here is the AJAX request written in jquery

The URL of the request is the defined href;

Data is returned after the request response;

Normally, we should request the information of all employees, and through the traversal here to get every employee's information such as ID, LastName, etc.

1.2. Here we need to introduce three jar packages

    1. Jackson-annotation-2.1.5.jar
    2. Jackso-core-2.1.5.jar
    3. Jackso-databind-2.1.5.jar

These three are used primarily for subsequent transformations of the returned data.

1.3. Add an interface to the handler Springmvctest

@ResponseBody

@RequestMapping ("Testjson") public

collection<employee> Testjson () {

  return Employeedao.getall ();

} 

My personal understanding here is that all of the employee information that is queried through the EmployeeDAO is returned to the interface as a response, and eventually the data form of a JSON is obtained through a series of processing, and then the parsing is traversed in the foreground page. And the completion of all this is attributed to the annotation @responsebody.

Specifically, there are some internal converter to do these transformations, interrupt points in the interface method, enter the debugging

Select Dispatcherservlet, find This->handleradapters->elementdata, find Requestmappinghandleradapter in this array, To find Messageconverters, you can see that there are 7 converters.

The 7th Mappingjackson2httpmessageconverter Here is the converter that we loaded in after we added the three jar packs. As you can see, there are enough converters to handle different data types.

1.4 Adding links to index.jsp

<form action= "Testfileupload" method= "POST" enctype= "Multipart/form-data" >

  file: <input type= "File" Name= "file"/>

  Desc: <input type= "text" name= "Desc"/> <input type= "

  submit" value= "Submit"/>

</form><br/> 

The final upload results are as follows

2. File download

2.1 Prepare the download source

Under webcontent new files directory, put aaa.txt, as a download source

2.2 Add hyperlink as download entry in index.jsp

 
 

2.3 Adding an interface to the handler Springmvctest

@RequestMapping ("testresponseentity") public

responseentity<byte[]> testresponseentity (HttpSession session) throws ioexception{

  byte[] the BODY = null;

  ServletContext ServletContext = Session.getservletcontext ();

  InputStream in = Servletcontext.getresourceasstream ("/files/aaa.txt");

  BODY = new byte[in.available ()];

  In.read (body);

     

  Httpheaders headers = new Httpheaders ();

  Headers.add ("Content-disposition", "Attachment;filename=aaa.txt");

  Httpstatus statusCode = Httpstatus.ok;

  responseentity<byte[]> response = new Responseentity<> (body, headers, statusCode);

  return response;

} 

Start Tomcat, we can see aaa.txt really can download ~ ~ ~

All right, here we go, what have we done?

1. Support for internationalization

2. File Upload

3. File download

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.