虽然用了很久,不过感觉对与struts2框架还是没有太深入的理解,最近决定从建项目开始学学struts。。。
本地环境:win10 jdk 1.8 Tomcat9 maven 3.3.9 Eclipse (集成m2e)
新建项目
在eclipse中新建一个maven project,在新建时选择webapp。
如果新建的项目中,index文件报错,通常是runtime server没导入,可以通过右键项目 ->build path->config build path->java build path->Libraries->add Libraries->server rumtime ,选择你的tomcat即可,如果没有tomcat那就是tomcat没有配置好,需要另行配置。
新建完成后,项目Package Explorer如下图所示
在pom文件中写入如下配置,导入struts相关的jar包。
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
写好后到pom文件目录下通过命令行运行mvn install或者在eclipse内右键项目->Run As->Maven intall,就完成了相关jar包的导入。
写入配置
对于web应用开发,首先的入口就是web.xml。很多框架的配置都是从这里开始(逻辑上)。在web.xml中添加一个StrutsPrepareAndExecuteFilter,发往服务器的请求就会被这个filter所处理。我具体的配置如下:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
filter-mapping中的url-pattern标签里是/*表示对所有的request都进行处理,在一般会写成*.do或者*.action。
后面的welcome-file是一个首页,在访问web应用时就会进入。login.jsp没有使用绝对路径,,一般默认使用的目录是src/main/webapp,在这个目录下新建一个login.jsp就可以在运行程序的时候看到啦。(下文会创建这个jsp)
在web.xml中引入struts后,struts会默认在资源路劲下加载一个struts.xml的文件,读取里面的配置。我struts.xml的配置如下图:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/base">
<action name="login" class="struts2.UserAction" >
<result name="success">/index.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
</struts>
我只是简单配置了一个action,action就是struts中的核心,通过action的配置,struts可以控制程序的跳转。在login这个action中,访问的请求会被发往struts2.UserAction这个类。(下文会创建这个类)在这个Action类处理完请求后,根据他的返回值(这个类会返回string)确定下一步的行为,本例中,是用资源目录下的jsp去渲染窗口。也可以传递给其他action来处理。package是一个包,struts的包有蛮强大的功能,可以在包里为包中的action做一些特别的配置。
下面创建Action类,为了方便后续扩展,我在src/main/java这个资源文件下创建了名称为struts2的包,并将UserAction创建在这个包里,这个类的代码如下:
package struts2;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String execute(){
try {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username->"+username+" password->"+password);
if ("admin".equals(username)&&"123456".equals(password)) {
return SUCCESS;
}else {
return "login";
}
} catch (Exception e) {
}
return "login";
}
}
action类需要继承ActionSupport类,在配置没有申明的情况下,会默认调用action类的execute方法。
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="base/login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录" /> <input
type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
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>Hello Maven</title>
</head>
<body>
<p>大家好,欢迎进入Maven Struts2应用!</p>
</body>
</html>