Ubuntu中, Struts2.5.22 环境搭建并实测

在搭建Struts前, 请确保系统中的Servlet 等都正常

下载Struts-2.5.22-all.zip并解压 struts-2.5.22
将如下jar包放至 apache-tomcat-9.0.31/lib/ 中,
并在工程中引用:
Ubuntu中, Struts2.5.22 环境搭建并实测_第1张图片

注意, 需将servlet-api.jar引入

工程中结构如下:
Ubuntu中, Struts2.5.22 环境搭建并实测_第2张图片

源码:

HelloWorldAction.java

package com.xx;

public class HelloWorldAction{
   private String name;

   public String execute() throws Exception {
      if(name.isEmpty())
    	  return "error";
      else
    	  return "success";
   }
   
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

struts.xml
(forHelloName 可以自定义名字)





<struts>
	
	<package name="forHelloName" namespace="/" extends="struts-default">
		<action name="helloAct" class="com.xx.HelloWorldAction" method="execute">
		
			<result name="success">/HelloWorld.jspresult>
			<result name="index">/HelloWorld.jspresult>
			<result name="error">/Error.jspresult>
		action>
	package>
struts>

web.xml


<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_4_0.xsd" id="WebApp_ID" version="4.0">
	<display-name>StrutsTestProjdisplay-name>
	<welcome-file-list>
		<welcome-file>index.htmlwelcome-file>
		<welcome-file>index.htmwelcome-file>
		<welcome-file>index.jspwelcome-file>
		<welcome-file>default.htmlwelcome-file>
		<welcome-file>default.htmwelcome-file>
		<welcome-file>default.jspwelcome-file>
	welcome-file-list>

	<filter>
		<filter-name>struts2filter-name>
		<filter-class>
		org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
      filter-class>
	filter>

	<filter-mapping>
		<filter-name>struts2filter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>
web-app>

Error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



Error

HelloWorld.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



 

Hello World, <%=request.getParameter("name")%>

Welcome, ${name}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>




Insert title here



	

Welcome

truts2






Struts2 的基本执行流程
Ubuntu中, Struts2.5.22 环境搭建并实测_第3张图片

你可能感兴趣的:(Java)