struts2之helloworld

首先确保你使用的是Eclipse for EE.以及安装好了TomCat本地服务器.
如果你对struts2还是不够足够的了解,建议你参看我的另一篇博客以便于对struts2有一个基本的认识.
分享一个链接http://blog.csdn.net/L___123456789/article/details/52793073
接下来我们先建一个web的工程
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
在这里我们 Generate web.xml文件勾选对号。一会减少一个建立web.xml文件.
完成web工程的建立后,我们开始配置struts2
手工从http://struts.apache.org/download.cgi下载Struts2的核心支持 库 (jar文件);
这里写图片描述
解压该文件到该文件夹.进入后 点击apps文件夹
这里写图片描述
使用解压工具将blank.war文件解压到当前文件夹.然后点击进入WEB-INF
这里写图片描述
进入后点击进入lib文件夹
这里写图片描述
可以看到当前文件夹下所需要的所有jar文件.全选copy
这里写图片描述
复制到你所建立的工程的lib目录下.
这里写图片描述

这里写图片描述
web.xml的作用实际上就是程序的入口文件,它决定了你在刚进入网页是看到的视图.


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
  <display-name></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> 

接下来进入struts.xml的配置:
struts.xml实际上是完成了映射之间的配置关系,将输入和实际的action类联系起来.
给出相应的struts.xml的配置信息:
struts.xml实际上是完成了映射之间的配置关系,将输入和实际的action类联系起来.
给出相应的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>
    <package name="helloworld"  extends="struts-default">
        <action name="hello" class="com.tutorialspoint.struts2.HelloWorldAction" method="execute">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts> 

接下开给出视图的文件 :index.jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="hello" method="get">
        <input name="name" type="text" placeholder="这里输入姓名" size="22"
            required /> 
        <input type="submit" value="开始查询"/>
    </form>
</body>
</html>

hello.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   Hello World, <s:property value="name"/>
</body>
</html>

当然了这个时候还要给出真的controller的内容:


package com.tutorialspoint.struts2;
public class HelloWorldAction{
   private String name;
   public String execute() throws Exception {
      return "success";
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
}

这里写图片描述
右键整个工程->Run as->Run on sever
如果浏览器当中弹出如下内容:
这里写图片描述
输入你的姓名:
这里写图片描述
那么恭喜你,你的helloworld执行成功啦~congratulations!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值