使用idea快速搭建Struts2的Maven项目
首先使用intelij idea部署Maven项目
1.jar包引入 pom.xml
我们用的是阿里云
我用的是最新版的Struts2核心jar包和最新版的xwork jar包
只需引入这两个节点
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.8</version>
</dependency> <dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.8</version>
</dependency>
2.接下来书写web.xml
直接上全部代码
<!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>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.然后就是jsp页面的书写了(View视图书写),我用了两个页面,一个登陆页面,一个登陆成功页面
这是登陆页面,附加说明。我引入了javaee的jar包,不然
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
这会报错的。还有一种解决办法,就是在服务器引入Servletjar包
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page isELIgnored="false"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录</title>
<h1>登录</h1>
<s:form action="LoginAction" namespace="/" method="POST">
<s:textfield name="users.name"></s:textfield><s:fielderror fieldName="ename"></s:fielderror><br/>
<s:textfield name="users.pwd"></s:textfield><s:fielderror fieldName="epwd"></s:fielderror><br/>
<s:submit value="登录"></s:submit>
</s:form>
<s:debug></s:debug>
</head> <body>
</body>
</html>
这是成功页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page isELIgnored="false"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>成功</title>
</head>
<body>
<h1>登录成功欢迎${name}您的密码是${pwd}</h1>
</body>
</html>
4.书写我们的Action
package cn.curry.entity; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware; import javax.servlet.http.HttpSession;
import java.util.Map; /**
* Created by Curry on 2017/2/27.
*/
public class LoginAction extends ActionSupport {//还可以实现 Action
private Users users;//实体po
private Map<String,Object> map;
//效验
public void validate(){
if(users.getName().length()==0){
addFieldError("ename","用户名不为空");
}
if(users.getPwd().length()==0){
addFieldError("epwd","密码不为空");
} }
//使用Servlet API去向Session中放入数据
public String execute() throws Exception {
if (users.getName().equals("admin")&&users.getPwd().equals("1")){
//解耦
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("name",users.getName());
session.put("pwd",users.getPwd());
//解耦2
/*map.put("name",users.getName());
map.put("pwd",users.getPwd());*/
//耦合
/*HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute("name",users.getName());
session.setAttribute("pwd",users.getPwd());*/
return SUCCESS;//常亮
}else{
return INPUT;
}
} public Users getUsers() {
return users;
} public void setUsers(Users users) {
this.users = users;
} public void setSession(Map<String, Object> session) {
this.map=session;
}
}
4.1实体类
package cn.curry.entity; /**
* Created by Curry on 2017/2/27.
*/
public class Users {
private String name;
private String pwd; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
}
}
5.在classpath,写一个名称为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>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<action name="LoginAction" class="cn.curry.entity.LoginAction">
<result name="success">success.jsp</result>
<result name="login">index.jsp</result>
<result name="input">index.jsp</result>
</action>
</package>
</struts>
6.部署运行,笔者使用的事Tomcat8,jdk事1.8
页面效果如上图
第一次使用Struts2来写一个小例子,遇到问题也比较多。很多都是jar包问题,再加上idea用的不熟(之前用myeclipse),波折颇多,最后圆满成功。