这两天从对Struts一窍不通到成功运行HelloWorld,在SSH这条路上迈出了第一步。
下面我把我的第一个Struts程序放上来:
一、新建web project,配置文件等准备工作
1. 新建一个Web Project项目,这个不用多说了把。
2. 在WebRoot -> WEB-INF -> lib下导入jar包
3. 配置 web.xml、struts.xml、logging.properties文件
(1) 在WEBRoot -> WEB-INF 下新建web.xml,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>TestStruts2</display-name>
<welcome-file-list>
<welcome-file>index.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>
(2) 在 src 下新建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"></constant>
<package name="helloworld" namespace="/helloworld" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="hello" class="com.mike.struts.HelloWorldAction" method="execute">
<result name="success">/pages/HelloWorld.jsp</result>
</action>
</package>
</struts>
(3) 在 src 下新建logging.properties,代码如下:
org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers =java.util.logging.ConsoleHandler
二、 建立一个Action
代码如下:
package com.mike.struts;
public class HelloWorldAction {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() throws Exception {
return "success";
}
}
三、jsp界面代码
1. index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="helloworld/hello">
<label for="name">Please enter your name:</label><br>
<input type="text" name="name"/>
<input type="submit" value="Say Hello!"/>
</form>
</body>
</html>
2. 在 WebRoot -> WEB-INF 下,新建一个 pages 文件夹用来存放其它jsp文件,
在 pages 文件夹下,新建HelloWorld,jsp,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'HelloWorld.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
Hello World!
<s:property value="name"/>
</body>
</html>
四、整体预览
五、运行结果
1. 打开浏览器,输入url,http://localhost:8080/TestStruts2。
2. 输入内容
3. 点击提交
六、在编写过程中遇到的和需要注意的问题
1. Struts版本为2.5。
2. web.xml的所在位置要注意一下,博主最开始建错地方了。
3. 注意jsp页面上方别忘引入Struts2标签库 <%@ taglib prefix="s" uri="/struts-tags"%>。
4. 对于Struts2.xml配置namespace的详解(引荐其它博主):Struts2 Namespace_命名空间 - Hongten - 博客园
5. 编写完成后,运行出错。
原因是 lib 中的 jar包有一个struts2-rest-plugin-2.5.10.1.jar,把这个jar包删掉就好了。
七、心得体会
在学习Struts2的过程,坐我旁边的同学给予了我很大的帮助,很感激。万事开头难,下定决定很重要,坚持去做更重要!落下多少,就要更加努力多少补回来。希望自己可以不偷懒不懈怠的坚持编程之路。
——2017-10-19