Struts2.1.6配置

Struts2.1.6测试:
一、下载开发包http://struts.apache.org/download.cgi#struts216

二、将核心的7个开发包引入到工程中
1.         commons-logging-1.0.4.jar
2.         freemarker-2.3.13.jar
3.         ognl-2.6.11.jar
4.         struts2-core-2.1.6.jar
5.         xwork-2.1.2.jar
6.         commons-fileupload-1.2.1.jar
7.         commons-io-1.3.2.jar
如果要配置注解文件,还需引进(struts2-convention-plugin-2.1.6.jar)
三、配置web.xml
    <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>


四、编写一个用户实体类
package sjh.strut2_test;
public class UserAction {
    private String password;
    private String username;
      public String getPassword() {
       return password;
    }
    public void setPassword(String password) {
       this.password = password;
    }
    public String getUsername() {
       return username;
    }
    public void setUsername(String username) {
       this.userName = username;
    }
public String execute() throws Exception {
  if (username.trim().equals("zjh") && password.equals("123")) {
   return SUCCESS;
  } else {
   return ERROR;
  }
}
   }

五、在src目录中建立struts配置文件: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="login" extends="struts-default">
<action name="user" class="zjh.struts2_test.UserAction">
<result name="success">/loginSuc.jsp</result>
<result name="error">/logiFailed.jsp</result>
</action>
</package>
   </struts>
五、建立一个用户登录页面index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<form action="user.action" method="post">
    姓名:<input type="text" name="username" style="width:150px"><br>
    密码:<input type="password" name="password" style="width:150px"><br>
    <input type="submit" value="登陆">
    </form>
</body>
</html>
  
六、建立一个用户登录成功页面loginSuc.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<h1>欢迎登陆:<s:property value="username"/></h1>
</body>
</html>  
七、建立一个用户登录失败页面loginFailed.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<h1>登陆失败</h1>
</body>
</html>  

你可能感兴趣的:(apache,freemarker,jsp,struts,XHTML)