Eclipse搭建struts2框架

新建动态web项目;

Eclipse搭建struts2框架_第1张图片

输入项目名称;

Eclipse搭建struts2框架_第2张图片 

New Runtime后选择如下,

Eclipse搭建struts2框架_第3张图片 

选择tomcat的安装目录;

Eclipse搭建struts2框架_第4张图片 

然后如下,完成;

Eclipse搭建struts2框架_第5张图片 

当前的目录结构如下;之前要配置好java环境;

Eclipse搭建struts2框架_第6张图片 

如果创建项目时没有选中生成web.xml,右击项目文件夹,选择如下菜单,生成web.xml;

Eclipse搭建struts2框架_第7张图片 

把struts2的包拷贝到lib文件夹下,然后刷新一下,会显示出来;

Eclipse搭建struts2框架_第8张图片 

选中所有的包,右击,选择如下菜单,把包加入项目;

Eclipse搭建struts2框架_第9张图片 

在src文件夹下创建struts.xml,

Eclipse搭建struts2框架_第10张图片 

struts.xml,





   
    
    
         
            /success.jsp
            /error.jsp
        
    

右击src文件夹,创建新的包, 

Eclipse搭建struts2框架_第11张图片 

输入包名,

Eclipse搭建struts2框架_第12张图片 

Finish之后如下,

Eclipse搭建struts2框架_第13张图片 

右击新创建的包,创建新的类,

Eclipse搭建struts2框架_第14张图片 

输入类名;

Eclipse搭建struts2框架_第15张图片 

package com.example.struts2;

import javax.servlet.http.HttpServletRequest; 
import org.apache.struts2.ServletActionContext;
  
public class LoginAction {
     
    HttpServletRequest req = ServletActionContext.getRequest();
    String username = req.getParameter("username");
    String password = req.getParameter("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 login(){
        if("xiaoBaby".equals(username)
                && "123456".equals(password)){
            return "result";
        }else{
            return "error";
        }
    }
     
}

然后再创建3个jsp;

index.jsp,

<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>




Insert title here

  
    
用户名: 密码:

success.jsp和error.jsp简单化,在之间添加一个字符串即可; 

 此时项目结构如下,

Eclipse搭建struts2框架_第16张图片 

还有web.xml代码;



  sts
  
  
    struts2
    org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

  
  
    struts2
    /*
  
  
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  

然后右击项目文件夹,选择如下菜单,

Eclipse搭建struts2框架_第17张图片 

出现如下对话框,Finish;

Eclipse搭建struts2框架_第18张图片 

index.jsp不会出来,查看控制台输出,包含一个错误,

    java.lang.ClassNotFoundException: .apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,

我想把web.xml中的

org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter

改为

   
            org.apache.struts2.dispatcher.FilterDispatcher
   


然后再重新运行,又出现下图错误;有时间继续;

Eclipse搭建struts2框架_第19张图片 

你可能感兴趣的:(Java,eclipse,struts,java)