搭建简单的struts框架

一、下载struts2所需jar包并导入到工程中(WEB-INF的lib文件夹下)

        地址:https://pan.baidu.com/s/1xkWDf4cjiSS-Vk1QCz3wBg

二、建立一个web工程

      

三、配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>StrutsDemo</display-name>
    <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
     <!-- 定义struts2 的核心控制器,用于生成ActionMapper ,拦截所有的Action请求-->
     <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> 
   
 </web-app>

四、编写Action类

public class LoginAction  extends ActionSupport {
    
    private static final long serialVersionUID = -6461652240745739673L;
    private String userName;
    private String password;
    
    public String execute() {
       if (userName.equals("zf") && password.equals("123")) {
            return SUCCESS;
        } else {
            return ERROR;
        }
     
    }

    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;
    }
   
}

五、建立struts.xml文件(放入Action所在包统计目录,名称必须为struts.xml)

     

        

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

 <package name="defult"  extends="struts-default">
    <action name="login" class="cn.zf.action.LoginAction">
       <result name="success">/jsp/success.jsp</result>
       <result name="error">/jsp/error.jsp</result>
    </action>
 </package>

</struts>

六、编写测试页面(建一个jsp文件夹)

        login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
 <%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="login" method="post">
              用户名:<input type="text" name="userName"><br/>
              密    码:<input type="password" name="password"/><br/>
             <input type="submit" value="提交"/> 
            <input type="reset" value="重置"/>                  
     </form>
</body>
</html>

  error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <h1>登录失败!</h1>
</body>
</html>

  success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <h1>登录成功!</h1>
</body>
</html>

七、访问地址 

 

posted @ 2018-07-26 15:53  miss小张  阅读(143)  评论(0编辑  收藏  举报