struts2第一个HelloWorld项目

基本项目文件框架如下:

struts2第一个HelloWorld项目_第1张图片

 

对于需要的jar包,可以直接在Struts2.zip包下面的lib包里面拷贝,但是有些jar包却没有,可以直接在Struts2.zip下app文件夹下解压struts2-blank.war(可以修改后缀名为.zip or .rar 进行解压)

 

struts.xml

<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
          "http://struts.apache.org/dtds/struts-2.0.dtd">
         
<struts>
    <package name="default" extends="struts-default">
        <action name="hello" class="test.struts2.sun.HelloWorld">
            <result name="success">HelloWorld.jsp</result>
<!--            <result name="tutorial" type="redirect">/tutorial/test.action</result>-->
<!--            <result name="tutorial2" type="redirectAction">/tutorial/test.action</result>-->
        </action>
    </package>

 
</struts>

 

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
  <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>

 

HelloWorld.java

 

package test.struts2.sun;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport{
 
 private String message;
 private String userName;
 
 public HelloWorld(){}
 
 @Override
 public String execute() throws Exception {
  //return super.execute();
  setMessage("Hello "+getUserName());
  return "success";
 }
 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }
}

 

 

index.jsp

<html>
  <head>
       
    <title>My JSP 'index.jsp' starting page</title>
   </head>
 
  <body>
   <s:form action="hello">
         <s:textfield name="userName" label="User Name"> </s:textfield>
         <s:submit value="提交"></s:submit>
   </s:form>
  </body>
</html>

 

HelloWorld.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<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">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    <h1>
   
     <s:property value="message"/>
    </h1>
  </body>
</html>

你可能感兴趣的:(struts2第一个HelloWorld项目)