Struts2搭建

来源:互联网 发布:学生分班软件 编辑:程序博客网 时间:2024/05/01 16:17

简单给大家介绍一下struts2搭建流程:

 

具体步骤如下:

第一步:

     创建一个web项目,然后将struts2 的jar包复制到lib下

第二步:

     创建struts.xml文件,此文件位于src下

  

    内容如下:

    <?xml version="1.0" encoding="GBK" ?>
  <!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"></include>
         <package name="first" namespace="/" extends="struts-default">
          <action name="login" method="login" class="com.hxmy.entity.LoginAction">
                  <result >/index.jsp</result>
         </action>
          </package>
</struts>

 

第三步,

配置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">
 <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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

第四步:

创建action

内容如下package com.hxmy.entity;

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.apache.struts2.ServletActionContext;

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

 

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 login(){
  
  System.out.println("运行到这里了");
  return ActionSupport.SUCCESS;
 }

 
}

完成,运行http://localhost:8080/项目名称/login.action