struts2_006

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.i18n.encoding" value="utf-8"></constant>
	<constant name="struts.custom.i18n.resources" value="login"></constant>

	<package name="base" extends="struts-default">
		<global-results>
			<result name="error">/error.jsp</result>
		</global-results>
		
		<global-exception-mappings>
			<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
		</global-exception-mappings>
	</package>
	
	<package name="test" namespace="/test" extends="base">
		<action name="exceptionAction" class="action.ExceptionAction">
			
		</action>
		
		<action name="loginAction" class="action.LoginAction">
			<result name="success">${forward}</result>
		</action>
	</package>
</struts>    



#login lable info
ur=User Register
un=Username
pwd=Password
submit=Submit
en=English
zh=Chinese

#error info
errorName= Username - {0} is error!
errorPwd= Password is error!
success = {0} login success!



ur=\u7528\u6237\u767B\u5F55
un=\u7528\u6237\u540D
pwd=\u5BC6\u7801
submit=\u63D0\u4EA4
en=\u82F1\u6587
zh=\u4E2D\u6587
errorName=\u7528\u6237\u540D - {0} \u4E0D\u6B63\u786E\!
errorPwd=\u5BC6\u7801\u4E0D\u6B63\u786E\!
success={0}\u767B\u5F55\u6210\u529F\!



package action;

import com.opensymphony.xwork2.ActionSupport;

public class ExceptionAction extends ActionSupport{

	public String test(){
		int i = 5/0;
		System.out.println(i);
		return SUCCESS;
	}
}



package action;

import com.opensymphony.xwork2.ActionSupport;

import entity.User;

public class LoginAction extends ActionSupport {

	private User user;
	private String forward;

	public String login() {

		forward = "/success.jsp";
		if (!user.getUsername().equals("admin")) {
			this.addFieldError("error_name", this.getText("errorName",
					new String[] { user.getUsername() }));
			forward = "/login.jsp";
		} else if (!user.getPassword().equals("123")) {
			this.addFieldError("error_pwd", this.getText("errorPwd",
					new String[] { user.getPassword() }));
			forward = "/login.jsp";
		}

		return SUCCESS;
	}
	
	public String change(){
		forward = "/login.jsp";
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getForward() {
		return forward;
	}

	public void setForward(String forward) {
		this.forward = forward;
	}
	
	

}



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'error.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  	抛出异常了,你不懂,我来管,去干别的事吧!
  </body>
</html>



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    This is my JSP page. <br>
  </body>
</html>



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  	<s:form action="test/loginAction!login" theme="simple">
  		<s:text name='ur' /><br>
  		<s:text name='un' />: <s:textfield name="user.username"></s:textfield><s:property value="errors.error_name"/><br>
  		
  		<s:text name='pwd' />:<s:password name="user.password"></s:password><s:property value="errors.error_pwd"/><br>
  		
  		<input type="submit" value="<s:text name='submit' />" /><br>
  	</s:form>
  	
  	<a href="test/loginAction!change?request_locale=en_US"><s:text name='en' /></a>
  	<a href="test/loginAction!change?request_locale=zh_CN"><s:text name='zh' /></a>
  	
  	<s:debug></s:debug>
  </body>
</html>



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <s:text name="success">
    	<s:param value="user.username"></s:param>
    </s:text>
  </body>
</html>

猜你喜欢

转载自xukongmoji.iteye.com/blog/1296962
006