Stuts2学习——HelloWorld

  这两天从对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,代码如下:  

  
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"  
 4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
 5     version="3.1">
 6   <display-name>TestStruts2</display-name>
 7   <welcome-file-list>
 8     <welcome-file>index.jsp</welcome-file>
 9   </welcome-file-list>
10   <filter>
11     <filter-name>Struts2</filter-name>
12     <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
13   </filter>
14   <filter-mapping>
15     <filter-name>Struts2</filter-name>
16     <url-pattern>/*</url-pattern>
17   </filter-mapping>
18 </web-app>
web.xml

  (2) 在 src 下新建struts.xml,代码如下:

  
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC  
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"  
 4     "http://struts.apache.org/dtds/struts-2.5.dtd">
 5 <struts>  
 6 <constant name="struts.enable.DynamicMethodInvocation" value="true"/>  
 7 <constant name="struts.devMode" value="true"></constant>  
 8     <package name="helloworld" namespace="/helloworld" extends="struts-default">  
 9         <global-allowed-methods>regex:.*</global-allowed-methods>  
10         <action name="hello" class="com.mike.struts.HelloWorldAction" method="execute">  
11             <result name="success">/pages/HelloWorld.jsp</result>  
12         </action>
13     </package>  
14 </struts>
struts.xml

  (3) 在 src 下新建logging.properties,代码如下:

  
org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers =java.util.logging.ConsoleHandler
logging.properties

  二、 建立一个Action

  

  代码如下:

  
 1 package com.mike.struts;
 2 
 3 public class HelloWorldAction {
 4     private String name;
 5 
 6     public String getName() {
 7         return name;
 8     }
 9 
10     public void setName(String name) {
11         this.name = name;
12     }
13     
14     public String execute() throws Exception {
15         return "success";
16     }
17     
18 }
HelloWorld.java

  三、jsp界面代码

  1. index.jsp

  
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib prefix="s" uri="/struts-tags"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'index.jsp' starting page</title>
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19   </head>
20   
21   <body>
22       <form action="helloworld/hello">
23         <label for="name">Please enter your name:</label><br>
24         <input type="text" name="name"/>
25         <input type="submit" value="Say Hello!"/>
26     </form>
27   </body>
28 </html>
index.jsp

  2. 在 WebRoot -> WEB-INF 下,新建一个 pages 文件夹用来存放其它jsp文件,

   在 pages 文件夹下,新建HelloWorld,jsp,代码如下:

  
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@ taglib prefix="s" uri="/struts-tags"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'HelloWorld.jsp' starting page</title>
14     
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">    
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20   </head>
21   
22   <body>
23     Hello World!
24     <s:property value="name"/>
25   </body>
26 </html>
HelloWorld.jsp

   四、整体预览

  

  五、运行结果

  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的详解(引荐其它博主):http://www.cnblogs.com/hongten/archive/2011/07/29/2121450.html

  5. 编写完成后,运行出错。

  

  原因是 lib 中的 jar包有一个struts2-rest-plugin-2.5.10.1.jar,把这个jar包删掉就好了。

  

  七、心得体会

  在学习Struts2的过程,坐我旁边的同学给予了我很大的帮助,很感激。万事开头难,下定决定很重要,坚持去做更重要!落下多少,就要更加努力多少补回来。希望自己可以不偷懒不懈怠的坚持编程之路。

原文地址:https://www.cnblogs.com/overfly/p/7693369.html