eclipse下简单配置struts2.5.8

1.下载structs2.5.8jar包。

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

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


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


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

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



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



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



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




web.xml

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1" metadata-complete="true">


	<display-name>Welcome to Tomcat</display-name>
	<description>
     Welcome to Tomcat
  </description>


	<welcome-file-list><!-- 定义主界面 -->
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
	<filter>
	    <filter-name>struts2</filter-name> <!-- 过滤器的名字 -->
	    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 引用个具体类文件 -->
	</filter>
	<filter-mapping>
	    <filter-name>struts2</filter-name>
	    <url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>



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

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

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

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

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

  5. <filter-class>引用的具体类文件名。一般引用搜索官方包装好的,名字固定。

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


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



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">
<struts>
	<include file="struts-default.xml"></include>
	<!-- 指定默认编码集 -->
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	<!-- 指定需要Struts2处理的请求后缀 -->
	<constant name="struts.action.extension" value="do,action"></constant>
	<!-- 设置浏览器是否缓存静态内容,开发阶段应关闭,生产阶段打开,默认为打开 -->
	<constant name="struts.serve.static.browserCache" value="false"></constant>
	<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段应打开 -->
	<constant name="struts.configuration.xml.reload" value="true"></constant>
	<!-- 开发模式下使用,可以打印出更详细的错误信息 -->
	<constant name="struts.devMode" value="true"></constant>
	<!-- action全部用注解进行配置 -->

	<!-- 是否开启动态方法调用 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<!--添加包 -->
	<!--<package name="tutorial" extends="struts-default">
		<action name="p" class="JavaSource.AllPersonAction" method="show">
			<result name="all">all.jsp</result>
			<result name="error">error.jsp</result>
		</action>
	</package>  -->
</struts>  

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


进行测试:

新建一个index.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>Insert title here</title>
</head>
<body>
	<form action="login.action" method="post">
		用户名:<input type="text" name="username">
		密码:<input type="text" name="password">
		<input type="submit" value="提交">
	</form>
</body>
</html>

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

error.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>Insert title here</title>
</head>
<body>
	Error!
</body>
</html>


success.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>Insert title here</title>
</head>
<body>
	Success!
</body>
</html>


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

package hellostruts;

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

public class login extends ActionSupport {
	HttpServletRequest req = ServletActionContext.getRequest();
	String username = req.getParameter("username");
	String password = req.getParameter("password");
	
	public String login() {
		if(username.equals("Leafage") && password.equals("123456")) {
			return "success";
		} else {
			return "error";
		}
	}
}

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

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

所以添加package后的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">
<struts>
	<include file="struts-default.xml"></include>
	<!-- 指定默认编码集 -->
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
	<!-- 指定需要Struts2处理的请求后缀 -->
	<constant name="struts.action.extension" value="do,action"></constant>
	<!-- 设置浏览器是否缓存静态内容,开发阶段应关闭,生产阶段打开,默认为打开 -->
	<constant name="struts.serve.static.browserCache" value="false"></constant>
	<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认为false,开发阶段应打开 -->
	<constant name="struts.configuration.xml.reload" value="true"></constant>
	<!-- 开发模式下使用,可以打印出更详细的错误信息 -->
	<constant name="struts.devMode" value="true"></constant>
	<!-- action全部用注解进行配置 -->

	<!-- 是否开启动态方法调用 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<!--添加包 -->
	<package name="hellostruts" namespace="/" extends="struts-default">
		<action name="login" class="hellostruts.login" method="login">
			<result name="success">success.jsp</result>
			<result name="error">error.jsp</result>
		</action>
	</package>
</struts>  

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

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

<constant name="struts.action.extension" value="do,action"></constant>

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

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


测试界面:


账号密码输入正确。




账号密码输入错误。




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




  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值