第一步 ,新建web project ,取名为Struts1 ,选择javaee6
第二步,点击完成以后的目录结构如下图所示
第三步,选中项目,选择MyEclipse --Project Capabilities--Add Struts Capabilities..
第四步,选择Struts2.1
第五步,选择Struts 2 Core Libraries包 ,点击完成
第六步,操作完成以后的目录结构如下图
到此为止 struts的框架就搭建完毕了。
下面开始开发 登陆功能
第一步,新建login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>yuewei‘Login</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
</head>
<body>
<form action="login.action" method="post">
User:<input type="text" name="username"><br>
Passoword:<input type="password" name="password"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
第二部,建立LoginAction
package com.sporttery.cn;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String username;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private boolean isInvalid(String value) {
return (value == null || value.length() == 0);
}
public String execute() {
if (isInvalid(getUsername()))
return INPUT;
if (isInvalid(getPassword()))
return INPUT;
if (this.getUsername().equals("wangyonglin1123")
&& this.getPassword().equals("wangyonglin1123")) {
ActionContext.getContext().getSession().put("user",getUsername());
ActionContext.getContext().getSession().put("password",getPassword());
return "success";
}
return "error";
}
}
第三部,配置stuts.xml
<?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>
<package name="authority" extends="struts-default">
<action name="login" class="com.sporttery.cn.LoginAction">
<result name="success">/welcome.jsp</result>
<result name="error">/login.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
第四部,创建welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> yuewei‘s Welcome</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
</head>
<body>
<h1>登录成功后显示此页面 ,欢迎你,${user} 你的密码是${password} </h1>
</body>
</html>
第五步,发布项目 启动项目,访问 结果如下图
拦截器的下一篇加上 ,谢谢大家有问题直接评论,我一一回复