In Struts1.x, garbled characters are processed and data is displayed through tags.

Source: Internet
Author: User

In Struts1.x, garbled characters are processed and data is displayed through tags.

1. Solve garbled characters through filter

Before a webpage request arrives, it must be filtered;

Request. setCharacterEncoding ("UTF-8 ");

Response Data garbled: response. setCharacterEncoding ("UTF-8 ");

Create a filter in Eclipse in two ways: annotation and xml

1) create a Filter Using Annotations:

Right-click a new filter, name it EncodingFilter, and click next. As shown in, configure the url /*

Click finish.

The Code is as follows:

 1 package util; 2  3 import java.io.IOException; 4 import javax.servlet.DispatcherType; 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest;10 import javax.servlet.ServletResponse;11 import javax.servlet.annotation.WebFilter;12 13 @WebFilter(dispatcherTypes = {14                 DispatcherType.REQUEST, 15                 DispatcherType.FORWARD, 16                 DispatcherType.INCLUDE, 17                 DispatcherType.ERROR18         }19                     , urlPatterns = { "/*" })20 public class EncodingFilter implements Filter {21 22     public void destroy() {}23 24     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {25         request.setCharacterEncoding("UTF-8");26         response.setCharacterEncoding("UTF-8");27         chain.doFilter(request, response);28     }29 30     public void init(FilterConfig fConfig) throws ServletException {}31 32 }

2) xml configuration
Java code:

 1 package util; 2  3 import java.io.IOException; 4  5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest;10 import javax.servlet.ServletResponse;11 12 public class EncodingFilter implements Filter {13 14     public void destroy() {}15 16     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {17         request.setCharacterEncoding("UTF-8");18         response.setCharacterEncoding("UTF-8");19         chain.doFilter(request, response);20     }21 22     public void init(FilterConfig fConfig) throws ServletException {}23 24 }

Configuration in web. xml

1   <filter>2       <filter-name>EncodingFilter</filter-name>3       <filter-class>util.EncodingFilter</filter-class>4   </filter>5   <filter-mapping>6       <filter-name>EncodingFilter</filter-name>7       <url-pattern>/*</url-pattern>8   </filter-mapping>

OK.

2. display data by TAG (example: show all users in userlist. jsp)

Write DAO code first

1/** 2 * Get information of all users 3 * @ return all users 4 */5 public ArrayList <User> getUsers () {6 ArrayList <User> users = new ArrayList <User> (); 7 String SQL = "select id, username, pwd from users"; 8 Connection conn = DBLib. getConn (); 9 try {10 PreparedStatement pstmt = conn. prepareStatement (SQL); 11 ResultSet set = pstmt.exe cuteQuery (); 12 while (set. next () {13 User user = new User (); 14 user. setId (set. getInt ("id"); 15 user. setUserName (set. getString ("username"); 16 user. setPwd (set. getString ("pwd"); 17 users. add (user); 18} 19 set. close (); 20 pstmt. close (); 21} catch (SQLException e) {22 e. printStackTrace (); 23} finally {24 try {25 conn. close (); 26} catch (SQLException e) {27 e. printStackTrace (); 28} 29} 30 return users; 31}

The page uses the struts tag for iterative output:

1 <% @ page language = "java" contentType = "text/html; charset = UTF-8" 2 pageEncoding = "UTF-8" import = "dao. * "%> 3 <% @ taglib uri =" http://struts.apache.org/tags-logic "prefix =" logic "%> 4 <% @ taglib uri =" http://struts.apache.org/tags-bean "prefix =" bean "%> 5 <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" http://www.w3.org/TR/html4/loose.dtd "> 6 

Logic labels are used for iteration, and bean labels are printed. I am not familiar with many labels, but I don't know much about it. Because there are a lot of labels, I need to ask the teacher or the old teacher again.

There is a reference link below: http://www.cnblogs.com/go-onxp/archive/2012/09/18/2690157.html

Bean: used to create bean, access bean, and access bean attributes;

Html: used to create a Struts input form;

Logic: used for logical judgment, set iteration, and process control;

Nested: Based on the first three tag libraries, with all the functions of the first three tag libraries, only allows nesting between tags;

Tiles: The page used to create the tiles style.

Generally, bean and logic are enough.

Code link: http://pan.baidu.com/s/1miTuwze extract code: kpa5

Related Article

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.