1.创建web项目,导入jar包
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.jar
xwork-core-2.1.6.jar
----------------------------web.xml--------------------------------
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns=" http://java.sun.com/xml/ns/javaee"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<display-name>Struts2.1</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
---------------------------struts.xml----------------------------------
3.在src下创建struts.xml,并配置
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
" http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 基本的配置内容 -->
<!-- package元素的name属性指的是包名,
extends属性表示继承
struts-default这个包 -->
<package name="struts2" extends="struts-default">
<!-- action元素的name属性值对应Form表单的action属性值,class属性表示要处理的那个action类,这里是提交给LoginAction处理 -->
<action name="login" class="com.test.action.LoginAction">
<!-- result元素的name属性值对应LoginAction中被调用的那个方法的返回值,根据返回值去匹配name属性,然后跳转到相应的JSP页面 -->
<result name="success">/index.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
</struts>
--------------------------- action ----------------------------------
4.创建action
**action类继承ActionSupport
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
@Override
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");
String type1 = request.getParameter("type");
System.out.println("用户名:"+ uname +" 密码:" + pwd + " 类型:"+ type1);
int type = 0;
if(null != type1 && !"".equals(type1)){
type = Integer.valueOf(type1);
}
Map errors = new HashMap();
if (uname == null || uname.trim().length() < 1) {
errors.put("unameError", "用户名必填");
}
if (pwd == null || pwd.trim().length() < 1) {
errors.put("pwdError", "密码必填");
}
if (type < 1 || type > 2) {
errors.put("typeError", "角色必填");
}
if (!errors.isEmpty()) {
request.setAttribute("errors", errors);
return ERROR;
}
try {
// userDao.validateUserLogin(uname, pwd, type);
request.setAttribute("uname", uname);
request.setAttribute("type", type);
return SUCCESS;
} catch (Exception e) {
errors.put("loginError", e.getMessage());
request.setAttribute("errors", errors);
return ERROR;
}
}
}
---------------------------login. jsp ----------------------------------
5.创建login页面
<body>
<form name="loginForm" method="post" action="login">
<div>${errors.loginError }</div>
用户名:
<input name="uname" type="text" value="${loginForm.uname}"/>
<span>${errors.unameError }</span>
<br />
密码:
<input name="pwd" type="password" />
<span>${errors.pwdError }</span>
<br />
角色:
<input type="radio" name="type" value="1" ${loginForm.type==1?"checked":""}/>
管理员
<input type="radio" name="type" value="2" ${loginForm.type==2?"checked":""}/>
会员
<span>${errors.typeError }</span>
<br />
<input type="submit" value="登录" />
</form>
</body>
---------------------------index jsp ----------------------------------
6.创建成功够的跳转页面 index页面
<body>
您好 ${type==1?"管理员":"会员" } ${ uname } <br>
</body>
7.测试
http://localhost:8080/项目名/login.jsp
OK !!!