IDEA 和 Struts2 参考其他资料的 HelloWorld
基本环境
- JAVA 配置
- Tomcat 配置
工程创建
- 下载 Struts 2
- 创建 Hello World 工程
- 修复 Project Setting
测试源码
- 添加测试源码
基本环境
已经熟悉如何配置则跳过基本环境配置。
工程默认 JAVA 设置
-
选择设置 File -> Other Settings -> Default Project Structure...
-
Project Settings -> Project
-
添加 JAVA 目录后完成设置
Tomcat 配置
-
选择设置 File -> Other Settings...
-
Build, Execution, Deployment -> Application Servers
-
点击 + 号, 选择 Tomcat Server
选择本地 Tomcat 目录后添加完成。
工程创建
- 首先到官网下载 Struts2 依赖
http://mirrors.tuna.tsinghua.edu.cn/apache/struts/2.3.34/struts-2.3.34-min-lib.zip
解压到 path_to_your_project/lib 下
struts_hello/lib
-
IDEA 选择 File -> New -> Project
-
选择 Java Enterprise -> Struts 2
-
Library 选择上面下载的 2.3 目录
也可以直接使用 Download 选项的 2.5,注意 2.5 版本依赖的 log4j
-
添加完成
-
选择目录
-
创建完成后 目录结构
-
打开工程设置
-
Project Settings -> Artifacts
可以看到有个 Problems
点击 Fix 按钮,选择 Add "Struts2" to the Artifacts;
或者
双击 Available Elements 窗口下的 Struts 2
将 Struts2 依赖添加到 部署输出目录中。
测试源码
在工程目录下添加下面文件
- src/beans/Production.java
package beans;
public class Production {
private int proId;
private String proName;
private String proDesc;
private String proPrice;
public Production(int proId, String proName, String proDesc, String proPrice) {
this.proId = proId;
this.proName = proName;
this.proDesc = proDesc;
this.proPrice = proPrice;
}
public Production() {
}
@Override
public String toString() {
return "Production{" +
"proId=" + proId +
", proName='" + proName + '\'' +
", proDesc='" + proDesc + '\'' +
", proPrice='" + proPrice + '\'' +
'}';
}
public int getProId() {
return proId;
}
public void setProId(int proId) {
this.proId = proId;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public String getProDesc() {
return proDesc;
}
public void setProDesc(String proDesc) {
this.proDesc = proDesc;
}
public String getProPrice() {
return proPrice;
}
public void setProPrice(String proPrice) {
this.proPrice = proPrice;
}
public String save() {
System.out.println("result:" + this);
return "detail";
}
}
- web/details.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>details</title>
</head>
<body>
productID:${proId}<br>
productName:${proName}<br>
productDesc:${proDesc}<br>
productPrice:${proPrice}<br>
</body>
</html>
- web/input.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>输入内容</title>
</head>
<body>
<form action="product-save.action" method="post">
productName:<input type="text" name="proName"><br>
productDesc:<input type="text" name="proDesc"><br>
productPrice:<input type="text" name="proPrice"><br>
<input type="submit" value="确定">
</form>
</body>
</html>
修改下面文件,具体内容如下
- web/WEB-INF/web.xm
<?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_3_1.xsd"
version="3.1">
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- web/index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>输入内容</title>
</head>
<body>
<form action="product-save.action" method="post">
productName:<input type="text" name="proName"><br>
productDesc:<input type="text" name="proDesc"><br>
productPrice:<input type="text" name="proPrice"><br>
<input type="submit" value="确定">
</form>
</body>
</html>
- src/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>
<!--<constant name="struts.devMode" value="true"/>-->
<package name="default" extends="struts-default">
<!--首页请求,name对应index.jsp中的action名字-->
<action name="product-input">
<!--转到input.jsp页面-->
<result>/input.jsp</result>
</action>
<!--第二个action请求,name对应input.jsp下的action名字
引用对象以及其方法-->
<action name="product-save" class="beans.Production" method="save">
<!--引用方法中返回的detai,对应name,展示到details下-->
<result name="detail">/details.jsp</result>
</action>
</package>
</struts>
添加完成后点击 Run
等待 Tomcat 启动成功,自动打开浏览器: