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
    评论
<?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> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="helloworld" class="com.mytest.HelloWorldAction"> <result> /result.jsp </result> </action> </package> <package name="LoginForm" extends="struts-default"> <action name="login" class="com.mytest.LoginAction" method="execute"> <result> /login.jsp </result> </action> </package> </struts> <!--1.使用 struts2.5.16 版本 2.lib 文件夹下放置:工程所需jar包 3.xml标签库为远程获取,路径:http://struts.apache.org/dtds/struts-2.5.dtd 可设置为本地【xml输入语法快捷提示】,就不用远程获取了:window-->preference-->输入Catalog-->xml下的Catalog-->Add-->location:解压缩struts-core-2.5.16.jar 后,文件struts-2.5.dtd文件路径。 4.设置开发者模式: <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="utf-8" /> 每次HTTP请求系统都重新加载资源文件,有助于开发 5.struts配置文件改动后,是否重新加载 <constant name="struts.configuration.xml.reload" value="true" /> 6.查看源码:Build path 后的类库中,奶瓶图标找到struts-core-2.5.16.jar 右键-->properties-->java Source Attachment-->External location :源码路径 7.查看文档API:Build path 后的类库中,奶瓶图标找到struts-core-2.5.16.jar 右键-->properties-->javadoc location :输入网址 或选择源码DOC目录 8.拦截器:web.xml 配置拦截器<filter> struts2.5的filter-class 与struts2.5以前版本有所不同 <!-- 浏览器访问 http://localhost:8080/MyWeb/helloworld --> --> <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置核心拦截器 --> <filter> <filter-name>struts2</filter-name> <!-- Filter的实现类 struts2.5 --> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <!-- 拦截所有的url --> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值