1.下载structs2.5.8jar包。

首先去官方下载structs2.5.8的jar包。

下载地址:http://struts.apache.org/download.cgi#struts258

eclipse下简单配置struts2.5.8

点击下载完成之后,将该压缩包进行解压。


2.在eclipse中配置使用jar包(已安装Tomcat服务器)。

首先打开eclipse,新建一个工程。

eclipse下简单配置struts2.5.8


然后打开刚才所解压的structs文件夹,找到其中的lib文件夹,里面存放了相关的jar包,在其中找到这些必须的包。

eclipse下简单配置struts2.5.8


将其导入到WebContent > WEB-INF > lib 目录下。

eclipse下简单配置struts2.5.8


然后配置web.xml文件,如果没有该文件,在WebContent > WEB-INF下创建一个即可,然后写入以下内容。

eclipse下简单配置struts2.5.8

eclipse下简单配置struts2.5.8


web.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3.   
  4. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee  
  6.                       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
  7.     version="3.1" metadata-complete="true">  
  8.   
  9.   
  10.     <display-name>Welcome to Tomcat</display-name>  
  11.     <description>  
  12.      Welcome to Tomcat  
  13.   </description>  
  14.   
  15.   
  16.     <welcome-file-list><!-- 定义主界面 -->  
  17.         <welcome-file>index.jsp</welcome-file>  
  18.     </welcome-file-list>  
  19.       
  20.     <filter>  
  21.         <filter-name>struts2</filter-name> <!-- 过滤器的名字 -->  
  22.         <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 引用个具体类文件 -->  
  23.     </filter>  
  24.     <filter-mapping>  
  25.         <filter-name>struts2</filter-name>  
  26.         <url-pattern>/*</url-pattern>  
  27.     </filter-mapping>  
  28. </web-app>  



附上一些filter中的配置说明:

  1. 首先<filter>...</filter>: 定义一个过滤器的意思

  2. 其次<filter-mapping>...</ filter-mapping> :配置上面那个定义的过滤器。

  3. <url-pattern>/*</url-pattern>表示适用的范围是所有的请求。

  4. <filter-name>过滤器的名字,可以自己取。

  5. <filter-class>引用的具体类文件名。一般引用eclipse下简单配置struts2.5.8官方包装好的,名字固定。

  6.  定义和配置即<filter>和<filter-mapping>是成对出现的。其中的<filter-name>相同则是一对。


然后在Java Resources > src 下新建一个名为:struts.xml文件,写入以下内容。

eclipse下简单配置struts2.5.8


struts.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE struts PUBLIC    
  3.             "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"    
  4.     "http://struts.apache.org/dtds/struts-2.5.dtd">  
  5. <struts>  
  6.     <include file="struts-default.xml"></include>  
  7.     <!-- 指定默认编码集 -->  
  8.     <constant name="struts.i18n.encoding" value="UTF-8"></constant>  
  9.     <!-- 指定需要Struts2处理的请求后缀 -->  
  10.     <constant name="struts.action.extension" value="do,action"></constant>  
  11.     <!-- 设置浏览器是否缓存静态内容,开发阶段应关闭,生产阶段打开,默认为打开 -->  
  12.     <constant name="struts.serve.static.browserCache" value="false"></constant>  
  13.     <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段应打开 -->  
  14.     <constant name="struts.configuration.xml.reload" value="true"></constant>  
  15.     <!-- 开发模式下使用,可以打印出更详细的错误信息 -->  
  16.     <constant name="struts.devMode" value="true"></constant>  
  17.     <!-- action全部用注解进行配置 -->  
  18.   
  19.     <!-- 是否开启动态方法调用 -->  
  20.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  21.     <!--添加包 -->  
  22.     <!--<package name="tutorial" extends="struts-default">  
  23.         <action name="p" class="JavaSource.AllPersonAction" method="show">  
  24.             <result name="all">all.jsp</result>  
  25.             <result name="error">error.jsp</result>  
  26.         </action>  
  27.     </package>  -->  
  28. </struts>    

其中最后的package就是所使用到的内容,可以根据自己的需求进行修改添加。


进行测试:

新建一个index.jsp文件。

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     <form action="login.action" method="post">  
  11.         用户名:<input type="text" name="username">  
  12.         密码:<input type="text" name="password">  
  13.         <input type="submit" value="提交">  
  14.     </form>  
  15. </body>  
  16. </html>  

新建一个error.jsp和successjsp文件,用来测试是否登录成功。

error.jsp:

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     Error!  
  11. </body>  
  12. </html>  


success.jsp:

[html] view plain copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     Success!  
  11. </body>  
  12. </html>  


新建一个Servlet类,用来将输入的用户名和密码进行测试,使用户名如果正确跳转到success页面,否则到error页面(继承ActionSupport与否都可以)。

[java] view plain copy
  1. package hellostruts;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. import javax.servlet.http.HttpServletRequest;  
  5. import org.apache.struts2.ServletActionContext;  
  6.   
  7. public class login extends ActionSupport {  
  8.     HttpServletRequest req = ServletActionContext.getRequest();  
  9.     String username = req.getParameter("username");  
  10.     String password = req.getParameter("password");  
  11.       
  12.     public String login() {  
  13.         if(username.equals("Leafage") && password.equals("123456")) {  
  14.             return "success";  
  15.         } else {  
  16.             return "error";  
  17.         }  
  18.     }  
  19. }  

然后修改刚才的struts.xml中的package内容,将Servlet添加进去。

package标签就是一组action,name属性可以修改,但必须是唯一的,extends建议不要修改,namespace属性默认是“/”,表单提交可以写成"/hello",如果修改成“/tset”,则表单提交的时候需要写成“/test/hello”。

所以添加package后的struts.xml文件内容如下:

[html] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE struts PUBLIC    
  3.             "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"    
  4.     "http://struts.apache.org/dtds/struts-2.5.dtd">  
  5. <struts>  
  6.     <include file="struts-default.xml"></include>  
  7.     <!-- 指定默认编码集 -->  
  8.     <constant name="struts.i18n.encoding" value="UTF-8"></constant>  
  9.     <!-- 指定需要Struts2处理的请求后缀 -->  
  10.     <constant name="struts.action.extension" value="do,action"></constant>  
  11.     <!-- 设置浏览器是否缓存静态内容,开发阶段应关闭,生产阶段打开,默认为打开 -->  
  12.     <constant name="struts.serve.static.browserCache" value="false"></constant>  
  13.     <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段应打开 -->  
  14.     <constant name="struts.configuration.xml.reload" value="true"></constant>  
  15.     <!-- 开发模式下使用,可以打印出更详细的错误信息 -->  
  16.     <constant name="struts.devMode" value="true"></constant>  
  17.     <!-- action全部用注解进行配置 -->  
  18.   
  19.     <!-- 是否开启动态方法调用 -->  
  20.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  21.     <!--添加包 -->  
  22.     <package name="hellostruts" namespace="/" extends="struts-default">  
  23.         <action name="login" class="hellostruts.login" method="login">  
  24.             <result name="success">success.jsp</result>  
  25.             <result name="error">error.jsp</result>  
  26.         </action>  
  27.     </package>  
  28. </struts>    

其中的action name就是映射的URL(可以通过name.do或者name.action访问),class 就是需要使用的类,而method就是其中调用的方法。

如果不想使用后缀.do或者.action的话,将strtus.xml中的

[html] view plain copy
  1. <constant name="struts.action.extension" value="do,action"></constant>  

删除,就可以直接使用action标签中的name值来访问了。

若最后返回的是success,则跳转到success.jsp;若是error,则跳转到error.jsp页面。


测试界面:


账号密码输入正确。

eclipse下简单配置struts2.5.8

eclipse下简单配置struts2.5.8


账号密码输入错误。

eclipse下简单配置struts2.5.8

eclipse下简单配置struts2.5.8


如果配置完成之后,确定无误却提示404资源未找到,可以尝试重启eclipse和tomcat试试看。


相关文章:

  • 2021-12-03
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
  • 2022-01-28
  • 2021-07-18
  • 2021-11-01
  • 2021-09-27
猜你喜欢
  • 2022-01-08
  • 2021-06-22
  • 2021-11-07
  • 2021-11-16
  • 2022-01-06
  • 2021-06-16
  • 2021-08-26
相关资源
相似解决方案