首先确保你使用的是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!