步骤一:创建Web Project
步骤二:新项目上右击-->MyEclipse-->Add Struts Capabilities...
步骤三:选择Struts版本为Struts 2.1 ,URL pattern为:/*
步骤四:
创建jsp文件,并修改index.jsp文件
login.jsp:使用Struts 2标签库实现一个表单
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!—导入Struts 2标签库--> <html> <head> <title>用户登录</title> </head>
<s:form action="checkLogin" namespace="/login"> <s:textfield name="username" style="font-size:12px; width:120px;" label="登录名称" /> <s:password name="password" style="font-size:12px; width:120px;" label="登录密码" /> <s:submit value=" 登 录 " /> </s:form> </body> </html> index.jsp:创建欢迎页面<body>
<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>欢迎</title> </head> <body> 登录成功,欢迎您! </body> </html>
步骤五:创建一个Action
import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class LoginAction extends ActionSupport { private String username; private String 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; } public String checkLogin(){ if(this.username.equals("admin")&&this.password.equals("admin")){ return SUCCESS; }else{ return LOGIN; } } }
步骤六:配置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> <include file="struts-default.xml"/> <package name="struts2_login" extends="struts-default" namespace="/login"> <action name="checkLogin" class="act.LoginAction" method="checkLogin"> <result name="success">/index.jsp</result> <result name="login">/login.jsp</result> </action> </package> </struts>
注:struts.xml配置模板存放在Struts2资源包\struts-2.1.8\apps\struts2-blank-2.1.8\WEB-INF\classes