java框架之struts2环境搭建

来源:互联网 发布:分享与存储系统源码 编辑:程序博客网 时间:2024/05/29 10:36

jar包导入:http://pan.baidu.com/s/1dFBd5Dn

Struts2 开发步骤。
第一步:导入相关struts2的jar包


第二步:在web.xml 添加过滤器来注册struts2

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
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_3_0.xsd">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 配置struts2过滤器 -->
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
  org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>

  
</web-app>

第三步:创建Action,继承com.opensymphony.xwork2.ActionSupport。

package com.dx.action;
import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport{

private static final long serialVersionUID = 1L;
private String name;
private String password;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public String execute()throws Exception{
return "success";
}

}

第四步:创建struts.xml文件配置action


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>   

    <package name="default" extends="struts-default">

        <action name="login" class="com.dx.action.LoginAction">
            <result name="success">success.jsp</result>
        </action>

    </package>
    
</struts>


第五步:创建view(jsp)。

0 0