Struts2.5.2 Step By Step

1. 下载struts-2.5.2-all

2. 在Eclipse中新建Dynamic Web Project,过程参见 http://running2016.iteye.com/admin/blogs/2329424

3. 引入Struts2.5.2工程必须的jar包:

commons-fileupload-1.3.2.jar

commons-io-2.4.jar

commons-lang3-3.4.jar

commons-logging-1.1.3.jar

freemarker-2.3.23.jar

javassist-3.20.0-GA.jar

log4j-api-2.5.jar

ognl-3.1.10.jar

struts2-core-2.5.2.jar

以上信息参考 blog.csdn.net/weiqingli190949353/article/details/52678109

4. 完整的配置文件和代码



 

4.1 配置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>Struts2</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>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
      <filter-name>struts-prepare</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
  </filter>
  <filter>
      <filter-name>struts-execute</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts-prepare</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  <filter-mapping>
      <filter-name>struts-execute</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts2早期版本的filter-class是org.apache.struts2.dispatcher.FilterDispatcher 或 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

2.5的是org.apache.struts2.dispatcher.filter.StrutsPrepareFilter,最好的办法是看struts2的发布包中的例子是用的哪个,一定要让struts2的版本和filter class对应起来,否则就会出现“HTTP Status 404”错误。

4.2 配置struts.xml

<?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">

<!-- START SNIPPET: xworkSample -->
<struts>

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

        <action name="login" class="com.checker.login.LoginAction" method="execute">
            <result name="success">/WEB-INF/welcome.jsp</result>
            <result name="error">/WEB-INF/error.jsp</result>
        </action>
        
    </package>


</struts>

4.3 LoginAction.java

package com.checker.login;

public class LoginAction {
    
    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 execute() throws Exception {
        
        if(this.getUsername().equals("admin")&&this.getPassword().equals("admin"))
        {
            return "success";
        }
        
        return "error";
        
    }

}

4.4 index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!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>
<s:form action="login" method="post">
    <s:textfield name="username" label="用户名"></s:textfield>
    <s:password name="password" label="密码"></s:password>
    <s:submit value="提交"></s:submit>
</s:form>
</body>
</html>

4.5 welcome.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!-- <%@taglib prefix="s" uri="/struts-tags" %>  -->
<%@ taglib prefix="s" uri="/struts-tags" %>

<!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>welcome</title>
</head>
<body>
Welcome <s:property value="username"/> !
</body>
</html>

4.6 error.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!-- <%@taglib prefix="s" uri="/struts-tags" %>  -->
<%@ taglib prefix="s" uri="/struts-tags" %>

<!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>error</title>
</head>
<body>
Sorry <s:property value="username"/>!
</body>
</html>

以上信息参考 www.cnblogs.com/labing/p/5883497.html

运行效果图

首页



 录入正确的密码



 录入错误的密码



 

猜你喜欢

转载自running2016.iteye.com/blog/2330683