web框架--struts2搭建实例

环境:tomcat7.0    jdk1.8    struts2.3.34
到官网下载struts2.3.34,解压,打开如图:

打开myeclipse,新建web project,将上面apps下struts2-blank.war解压,得到WEB-INF 等,
1.将WEB-INF下lib中的jar包copy到webrooot/WEB-INF/bin,右键build path->add to build path。
2.将WEB-INF下src/java中的struts.xml,log4j.xml,velocity.properties复制到web项目的src目录下。

新建index.jsp



<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
  <head>
    <title>登录</title>
  </head>
  
  <body>
  <form action="login" method="post">
    请登录:<br>
    <table>
    <tr>
    <td>用户名</td>
    <td><input type="text" name="username" /></td>
    </tr>
    <tr>
    <td>密    码</td>
    <td><input type="password" name="password" /></td>
    </tr>
    
    <tr>
    <td></td>
    <td>
    <input type="submit" value="登录" />
    </td></tr>
    </table>
   
    </form>
  </body>
</html>


修改web.xml,添加struts2核心拦截器:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>ssh</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

新建loginAction:
package Action;

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

public class loginAction extends ActionSupport{

   
	private static final long serialVersionUID = 1L;
	private String username;//账号
    private String password;//密码
    
    //getters & setters
    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 execute(){
        if(username.equalsIgnoreCase("can") && password.equalsIgnoreCase("123")){
        	ActionContext.getContext().getSession().put("user", getUsername());
            return SUCCESS;
        }
        else
            return ERROR;
    }

}

新建success.jsp和error.jsp:
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
  <head>
    <title>登陆成功</title>
  </head>
  
  <body>
    欢迎${sessionScope.user},登录成功!<br />
  </body>
</html>

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
  <head>
    <title>登录失败</title>
  </head>
  
  <body>
    登录失败!用户名或密码错误!
  </body>
</html>

至此struts2登陆实例完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值