【Struts2】Struts2框架的搭建

1,Struts2简介

struts1和struts2都是由Apache组织发布的,但是比较有趣的是struts2和struts1并没有“血缘关系”。在Apache发布struts1之后,当时是还是非常流行的,但是随着时间推荐,这时候struts1暴露的问题就越来越多了,在这个时候opensymphony组织发布了WebWork框架,WebWork框架在当时适应的很好。opensymphony组织由于向外推广的力度不够,使得WebWork不能很快的面向大众。这个时候Apache组织联合opensymphony组织发布了struts2,因此struts2和struts1联系并不大。在struts2刚发布出来的时候,许多的类库都是直接使用struts1的。

2,Struts2和SpringMVC对比

struts2和springMVC结构非常相似,都是基于MVC结构的。就连配置都很相似,关于SpringMVC和Spring的知识可以参见http://www.cnblogs.com/HDK2016/category/918254.html

下面给两种图片,分别是springMVC和struts2的结构流程图。

SpringMVC流程图:

struts2流程图:

通过这两张图片的对比可以看出。

struts2中的 StrutsPrepareAndExecuteFilter 相当于 SpringMVC中的 DispatcherServlet。

struts2中的 Action 相当于 SpringMVC 中的 Controller

struts2中的 result 相当于 SpringMVC 中的 ViewResovler

3,Struts2框架的搭建

首先应该下载jar包,将jar包导入到项目的lib目录下面。

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" version="3.0">
  <display-name>struts</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</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts</filter-name>
    <url-pattern>*</url-pattern>
  </filter-mapping>
</web-app>
web.xml

由于struts2默认只处理有以.action结尾或是无后缀的url,所以计算web.xml中过滤器不过滤url,也会有url的限制,一般在struts2项目中不再web.xml中过滤url。web.xml会默认在加载src目录下的struts.xml文件。注意文件名称不要改变,否则的话也可以在web.xml中指定struts.xml的位置。

struts.xml 文件

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

<struts>
      <!--Struts默认只会通过.action和无后缀的请求,我们可以通过指定extension来使得Struts只通过.do的URL的请求。-->
      <constant name="struts.action.extension" value="do"/>
      
    <!-- 必须继承struts-default,才能使用Result组件 -->
    <package name="user" extends="struts-default">
        <!-- name请求名;class是Action类名;method是Action方法名,默认execute -->
        <action name="userlogin" class="cn.test.controller.UserLogin">
            <!-- name对应action返回值;type指定Result 类型 -->
            <result name="result" type="dispatcher">loginResult.jsp</result>
        </action>
    </package>
</struts>
struts.xml

我们在struts.xml文件中使用<constant>覆盖了默认了文件扩展名,使得只处理以.do的url,如果想接受多个,那么以逗号隔开。比如: value="do,action,," ,就可以处理以.do,.action和无后缀的url了。

UserLogin.java 文件

package cn.test.controller;

public class UserLogin {

    /*
     *struts中规定,接受页面传过来的值和发送到页面中值都通过声明全局变量,然后通过setter和getter方法完成。 
     */
    private String username;//名字可要页面传过来的值一致,否则不能正确接受值
    private String password;
    
    private String resultmessage;//返回值
    
    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 getResultmessage() {
        return resultmessage;
    }
    public void setResultmessage(String resultmessage) {
        this.resultmessage = resultmessage;
    }
    
    public String execute(){
        
        if("张三".equals(username) && "123456".equals(password)){
            resultmessage="登录成功";
        }else{
            resultmessage="登录失败";
        }
        
        return "result";
    }
}
UserLogin.java

Struts2规定,从页面接受值和传送值到页面都是通过全局变量来实现的。然后控制器中方法的返回值只有两种,一种是Sting,另一中就是void。

login.jsp 文件

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="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=utf-8">
<title>用户登录</title>
</head>
<body>
<form action="userlogin.do" method="POST">

用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>

<input type="submit" value="提交"/>
</form>
</body>
</html>
login.jsp

loginResult.jsp 文件

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="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=utf-8">
<title>用户登录</title>
</head>
<body>
${resultmessage}
</body>
</html>
loginResult.jsp

到这里我们就配置一个简单的用户登录的struts2框架了。

原文地址:https://www.cnblogs.com/HDK2016/p/7308660.html