package yang.www;
import java.util.Locale;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ChangeLang extends ActionSupport
{
/**
*
*/
private static final long serialVersionUID = 1L;
private String language;
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String execute()
{
String[] strings = this.getLanguage().split("_");
Locale locale = new Locale(strings[0], strings[1]);
ServletActionContext.getContext().setLocale(locale);
ActionContext.getContext().getApplication().put("lang", this.getLanguage());
return SUCCESS;
}
}
//Login Action
package yang.www;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class Login extends ActionSupport implements ModelDriven<UserBean>
{
/**
*
*/
private static final long serialVersionUID = 1L;
private UserBean user = new UserBean();
@Override
public UserBean getModel() {
// TODO Auto-generated method stub
return user;
}
ActionContext context = ActionContext.getContext();
public String execute()
{
boolean rightUser = "yangzhiyong".equals( this.getModel().getUserName() );
boolean rightPassWord = "yangzhiyong".equals(this.getModel().getPassWord());
if ( rightUser && rightPassWord )
{
context.getSession().put("username", this.getModel().getUserName());
return SUCCESS;
}
else
{
return ERROR;
}
}
public void validate()
{
if (this.getModel().getUserName().length() < 6 )
{
this.addFieldError("userName", this.getText("label_userError"));
}
if (this.getModel().getPassWord().length() < 5)
{
this.addFieldError("passWord", this.getText("label_passwordError"));
}
}
}
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant value="zh_CN" />
<constant value="i18n/myMessage" />
<constant value="simple" />
<constant value="template" />
<constant value="ftl" />
<package extends="struts-default" namespace="/">
<action>
<result>/jsp/welcome.jsp</result>
<result>/jsp/error.jsp</result>
<result>/index.jsp</result>
</action>
<action >
<result>/index.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
//error.jsp