1、使用创建Maven项目
参考创建Maven标准项目(前一部分就是eclipse创建maven的详细步骤)
2、整合struts2框架
2.1 pom.xml文件添加struts2的核心依赖
<!-- Struts2的核心包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24</version>
</dependency>
2.2 web.xml文件添加struts2的核心过滤器
<!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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.3 创建Action处理类
package com.itheima.actions;
public class UserAction {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String execute() {
System.out.println("name:"+name);
System.out.println("age:"+age);
return "success";
}
}
2.4 创建struts.xml配置文件(注意别成struts2.xml!!!这样就会找不到这个文件了)
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="xxxx" namespace="/test" extends="struts-default">
<action name="myLogin" class="com.itheima.actions.UserAction">
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
2.5 编写index.jsp和welcome.jsp页面
//index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="test/myLogin.action" method="post">
姓名:<input type="text" name="name"/><br/>
年龄:<input type="text" name="age"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
//welcome.jsp
//注意:要设置isELIgnored="false",否则将忽略EL表达式,直接返回表达式原来的样子
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
姓名:${name }<br/>
年龄:${age }
</body>
</html>
2.6 测试
最后附上本项目结构:
总结
maven整合struts2并不难,只是将传统的到jar的方式改成了通过pom文件引入依赖,自动下载对应jar。其他要注意的点就是struts.xml这个文件的名字不能修改,另外jsp页面如果EL表达式没起作用应该是jsp页面关闭EL表达式功能。