WelcometohelloApp

Engl" />

带有自定义标签库的中英文页面

系统 2074 0
一个用于选择不同语言的JSP页面
    

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>
  <head>

    
    <title>welcome to hello jsp</title>


  </head>
  
  <body>
    <p><font size=7>Welcome to helloApp</font></p>
    <p><a href="WelcomeLogin.jsp?language=English">English Page</a></p>
    <p><a href="WelcomeLogin.jsp?language=Chinese">Chinese Page</a></p>
     </body>
</html>


  


带有自定义标签库的中英文页面


分别为中文和英文的Properties的文件
    
hello.title=helloapp
hello.hi=Nice to meet you
login.title=helloapp
login.user=Username
login.password=Password
login.submit=Submit

  
    
hello.title=helloapp
hello.hi=你好
login.title=helloapp的登录页面
login.user=用户名
login.password= 密码
login.submit=登录

  



用于加载中英文静态文本的Servelt类

    
package Tag1;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

	

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
         init();
         PrintWriter pw =response.getWriter();
         pw.println("The resouce file is reloaded");
		
	}

	
	public void init() throws ServletException {
		 Properties ps =new Properties();
		 Properties ps_ch=new Properties();
		 
		 ServletContext context =getServletContext();
		 
		 InputStream in=context.getResourceAsStream("/WEB-INF/message.properties");
		 InputStream in_ch=context.getResourceAsStream("/WEB-INF/message_ch.properties");
		 try {
			ps.load(in);
			ps_ch.load(in_ch);
			in.close();
			in_ch.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		context.setAttribute("ps", ps);
		context.setAttribute("ps_ch", ps_ch);
		 
	}

}


  


一个标签处理类

    
package Tag1;

import java.util.Properties;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class MessageTag extends TagSupport {
    private String key =null;

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	@Override
	public int doEndTag() throws JspException {
		try{
	    Properties ps =(Properties)pageContext.getAttribute("ps", pageContext.APPLICATION_SCOPE);
		
		Properties ps_ch=(Properties)pageContext.getAttribute("ps_ch", pageContext.APPLICATION_SCOPE);
		
		HttpSession session=pageContext.getSession();
		
		String language=(String)session.getAttribute("language");
		
		String message=null;
		if(language!=null&&language.equals("Chinese")){
			message =(String)ps_ch.get(key);
			message=new String(message.getBytes("ISO-8859-1"),"UTF-8");
		}else{
			message=(String)ps.get(key);
		}
		pageContext.getOut().println(message);
		
	
		}catch(Exception e){
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
     
    
}



  



一个标签库
     
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
 <tlib-version>1.0</tlib-version>
 <jsp-version>1.2</jsp-version>
 <short-name>MyTag</short-name>
 <uri>/MyTag</uri>
<tag>
   <name>MyTag</name>
   <tag-class>Tag1.MessageTag</tag-class>
   <body-content>empty</body-content>
   <attribute>
      <name>key</name>
      <required>true</required>
   </attribute>
 </tag>
</taglib>

  


根据选择 显示不同语言的JSP页面
    
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/MyTag.tld" prefix="cc"%>
<html>
   <%  
      String language=request.getParameter("language");	
      if(language==null)
    	  language="English";
      session.setAttribute("language",language);
     
   %>
  <head>
   <title><cc:MyTag key="login.title"/></title>
   </head>
  <body>
    <br>
    <form name="LoginForm" method="post" action="Welcomout.jsp">
        <cc:MyTag key="login.user"/>:<br>
        
        <input type="text" name="username"><br>
        <cc:MyTag key="login.password"/>:<br>
        <input type="password" name="password"/><br>
        <input type="submit" value="<cc:MyTag key="login.submit"/>"/><br/>
    </form>
  </body>
</html>


  


web.xml配置信息:
    
  <jsp-config>
  <taglib>
  <taglib-uri>/WEB-INF/MyTag.tld</taglib-uri>
  <taglib-location>/WEB-INF/MyTag.tld</taglib-location>
</taglib>
</jsp-config>

  

带有自定义标签库的中英文页面


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论