第一次搭建Sturts框架会遇到几个坑
1.IntelliJ自动生成的web.xml中filter-class报错,是因为Struts2-2.5版本修改了
改为
2.配置好Tomcat后会报错
可是我明明已经配正确了啊?修复的地方如下(这是最坑的地方)
回到我的Android Studio一看,果然真有一样的
3.接下来又会报个错,尼玛,错误我就不发了,反正是少了个jar包
添加图中的xwork-core-2.1.6.jar然后居然还得Fix一下,终于好了
我尼玛一行代码还没写啊,都折腾个半死,IntelliJ该好好反省一下
好了,下面开始讲Struts2的配置
我的Demo工程结构如下
先把公用的代码贴出来
struts.xml(一个工程只有一个)
代码语言:javascript复制<?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="com/jinke/struts2/demo1/struts_demo1.xml"/>
<include file="com/jinke/struts2/demo2/struts_demo2.xml"/>
<include file="com/jinke/struts2/demo3/struts_demo3.xml"/>
<include file="com/jinke/struts2/demo4/struts_demo4.xml"/>
<include file="com/jinke/struts2/demo5/struts_demo5.xml"/>
</struts>
web.xml(welcome-file是tomcat启动后自动弹出的首页,一个工程也只有一个)
代码语言:javascript复制<?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_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>demo.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>
第一部分:最基础的
代码语言:javascript复制public class HelloAction {
public String execute() {
System.out.println("helloaaction执行了");
return "success";
}
}
struts_demo1.xml
代码语言:javascript复制<?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>
<package name="hello" extends="struts-default" namespace="/">
<action name="hello" class="com.jinke.struts2.demo1.HelloAction">
<!--配置页面的跳转-->
<result name="success">success.jsp</result>
</action>
</package>
</struts>
index.jsp
代码语言:javascript复制<%--
Created by IntelliJ IDEA.
User: wanglei
Date: 2019/6/15
Time: 11:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Struts入门</title>
</head>
<body>
<h1>Struts2入门</h1>
<h3><a href="${ pageContext.request.contextPath }/hello.action">Struts2的入门</a></h3>
</body>
</html>
success.jsp
代码语言:javascript复制<%--
Created by IntelliJ IDEA.
User: wanglei
Date: 2019/6/15
Time: 12:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h1>跳转页面成功!</h1>
</body>
</html>
结果
第二部分:实现Action的三种方式
代码语言:javascript复制public class ActionDemo2 {
public String execute() {
System.out.println("ActionDemo2执行了");
return null;
}
}
代码语言:javascript复制import com.opensymphony.xwork2.Action;
/**
* action实现方法2:实现接口
*/
public class ActionDemo3 implements Action {
@Override
public String execute() throws Exception {
System.out.println("ActionDemo3执行了");
return null;
}
}
代码语言:javascript复制import com.opensymphony.xwork2.ActionSupport;
/**
* action实现方式3:继承actionSupport
*/
public class ActionDemo4 extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("actiondemo4执行了");
return NONE;
}
}
struts_demo2.xml
代码语言:javascript复制<?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>
<package name="demo2" extends="struts-default" namespace="/">
<action name="actionDemo2" class="com.jinke.struts2.demo2.ActionDemo2"/>
</package>
</struts>
struts_demo3.xml
代码语言:javascript复制<?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>
<package name="demo3" extends="struts-default" namespace="/">
<action name="actionDemo3" class="com.jinke.struts2.demo3.ActionDemo3"/>
</package>
</struts>
struts_demo4.xml
代码语言:javascript复制<?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>
<package name="demo4" extends="struts-default" namespace="/">
<action name="actionDemo4" class="com.jinke.struts2.demo4.ActionDemo4"/>
</package>
</struts>
第三部分:实现方法的三种方式
代码语言:javascript复制import com.opensymphony.xwork2.ActionSupport;
public class CustomerAction extends ActionSupport {
public String find() {
System.out.println("查询客户");
return NONE;
}
public String update() {
System.out.println("修改客户");
return NONE;
}
public String delete() {
System.out.println("删除客户");
return NONE;
}
public String save() {
System.out.println("保存客户");
return NONE;
}
}
代码语言:javascript复制import com.opensymphony.xwork2.ActionSupport;
public class ProductAction extends ActionSupport {
public String find() {
System.out.println("查询商品...");
return NONE;
}
public String update() {
System.out.println("修改商品...");
return NONE;
}
public String delete() {
System.out.println("删除商品...");
return NONE;
}
public String save() {
System.out.println("保存商品...");
return NONE;
}
}
代码语言:javascript复制import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
public String find() {
System.out.println("查询用户...");
return NONE;
}
public String update() {
System.out.println("修改用户...");
return NONE;
}
public String delete() {
System.out.println("删除用户...");
return NONE;
}
public String save() {
System.out.println("保存用户...");
return NONE;
}
}
struts_demo5.xml
代码语言:javascript复制<?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"/>
<package name="demo5" extends="struts-default" namespace="/">
<action name="userFind" class="com.jinke.struts2.demo5.UserAction" method="find"></action>
<action name="userUpdate" class="com.jinke.struts2.demo5.UserAction" method="update"></action>
<action name="userDelete" class="com.jinke.struts2.demo5.UserAction" method="delete"></action>
<action name="userSave" class="com.jinke.struts2.demo5.UserAction" method="save"></action>
<!--通配符方式-->
<action name="product_*" class="com.jinke.struts2.demo5.ProductAction" method="{1}">
<allowed-methods>find,update,delete,save</allowed-methods>
</action>
<!--动态方法访问的方式-->
<action name="customer" class="com.jinke.struts2.demo5.CustomerAction">
<allowed-methods>find,update,delete,save</allowed-methods>
</action>
</package>
</struts>
这里有个坑:struts2在2.5之后需要在Struts.xml中增加可用的方法
代码语言:javascript复制<allowed-methods>find,update,delete,save</allowed-methods>
结果