您的位置:首页 > 编程语言 > Java开发

Struts-登录-代码1

2016-01-05 11:15 375 查看
web.xml

<!-- 定义Struts2的核心控制器StrutsPrepareAndExcuteFilter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<!-- StrutsPrepareAndExcuteFilter用来处理所有的http请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


login.jsp

<s:form action="login">
<s:actionmessage />
<s:textfield name="username" label="username" />
<s:textfield name="password" label="password" />
<s:submit value="login" />
</s:form>


index.jsp

welcome to  ${sessionScope.user} user ,login success!
error.jsp

no ${sessionScope.user} user ,login error!


struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="com.struts2.action.LoginAction">
<result name="success">/index.jsp?user=${12}</result>
<result name="error">/error.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>


Login.Action

/*
* Creation : 8 Oct 2015
*/
package com.struts2.action;

import org.apache.commons.lang3.StringUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

/** The Constant serialVersionUID. */
private static final long serialVersionUID = 2078216757486534803L;

private String username;

private String password;

public String execute() throws Exception {
System.out.println("username:" + username + "--password:" + password);
ActionContext.getContext().getSession().put("user", username);
if (username.equals("11")) {
return SUCCESS;
}
return ERROR;
}

public void validate() {
if (StringUtils.isEmpty(username)) {
addFieldError("username", "username");
addActionMessage("awsd");
}
if (StringUtils.isEmpty(password)) {
addFieldError("password", "password");
}
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: