struts的配置,相当简单,我采用的是maven将struts依赖的包导入到项目中。
第一步:
·在Eclipse下安装MAVEN,具体教程就不详细说了,百度安装MAVEN。
·然后新建maven项目
完成后,就有一个新的项目空间在左边Package Explore中
第二步:
配置pox.xml文件
加入struts的maven依赖内容,然后maven会帮我们自动下载struts需要的jar包:
· <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.8</version>
</dependency>
第三步:
配置web.xml,web.xml文件在WEB-INF下面,如果没有可以在此目录下新建xml文件,命名为web.xml,其中内容(重点为红色内容),所有的浏览器发出的Http请求到web都必须进过web.xml配置的struts的filter(首选将请求拦下来):
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd
"
id="WebApp_ID"
version="3.1">
<filter>
<filter-name>strutName</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>strutName</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
第四步:
编写请求的测试页面:index.jsp (注意在webapp目录下面,如何新建jsp,鼠标点击文件夹webapp,然后window按crtl+N,调出新建向导,搜索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>
<title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2</h1>
<form action="login.action" method="post">
用户名:<input type="text" name="username">
密码:<input type="text" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
(解释:action:就是你点击提交这个input按钮后,浏览器的网页地址栏,就变为了localhost/test/login.action。也就是向服务器发出一个请求行为,然后web.xml的filter就会拦截到,并且分发到login.action该去的地方,那么它该去哪儿呢?
第五步:指定action该去哪儿。
具体步骤:
一、新建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开头的标签都是一些对struts的要求内容,比如要求使用编码方式啊,比如struts.xml文件在运行时更改了,是否reload啊 -->
<include file="struts-default.xml"></include>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 下面这行要求action以do和action结尾 -->
<constant name="struts.action.extension" value="do,action"></constant>
<constant name="struts.configuration.xml.reload" value="true"></constant>
<constant name="struts.devMode" value="true"></constant>
<!-- 下面com.struts2.demo是我的包名,也就是HelloWorldAction的包名,namespace是项目根名-->
<packagename="com.struts2.demo" namespace="/" extends="struts-default">
<!-- 下面action的name就是刚刚index.jsp提交过来的action,class对应要响应这个action的java文件,method表示响应java文件的具体的响应方法-->
<action name="login"class="com.struts2.demo.HelloWorldAction" method="execute">
<!--如果调用execute返回的字符串是success,就发给客户端success.jsp这个页面,如果返回字符串是error,就发给客户端error.jsp -->
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
下面截图是项目根名,改为 / 。
最后一步:
编写HelloWorldAction.java
package com.struts2.demo;
import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport implements Serializable {
HttpServletRequest request = ServletActionContext.getRequest();
// 的请求的输入框的username,和password。getParameter("这是index.jsp中两个input的name")
String username = request.getParameter("name");
String password = request.getParameter("password");
public String execute() throws Exception {
return "success";
}
}//execute方法被调用,返回success给struts,然后,struts查看strut.xml,确认应该跳转到页面:success.jsp