`
雪馨25
  • 浏览: 128416 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

跟着官网学Struts2使用(二)

阅读更多

有了上一节的成功尝试,我明显感觉信心倍增,自信满满,事不宜迟,继续加油咯

这一次,是动手实验篇,我偏重于在教程中找到重点信息,摘录备忘,以及记录整个实现过程;单词都是很简单的,加油

学习地址:http://struts.apache.org/2.2.1/docs/hello-world-using-struts-2.html

实现目标:Suppose you  want  to create a simple "Hello World" example that displays a welcome message.
    1.Create a class to store the welcome message (the model)
    2.Create a server page to present the message (the view)
    3.Create an Action class to control the interaction between the user, the model, and the view (the controller)
    4.Create a mapping (struts.xml) to couple the Action class and view
Step 1 - Create The Model Class  MessageStore.java
create the MessageStore class in src/main/java. Be sure to note the package statement below.(在项目目录下的src/main/目录下建立java文件夹,并且确保java文件的包结构正确)

package org.apache.struts.helloworld.model;

public class MessageStore {
	private String message;
	public MessageStore() {	
		setMessage("Hello Struts User");
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
}

In the model class above note the use of public set and get methods to allow access to the private message String attribute.
Step 2 - Create The Action Class  HelloWorldAction.java
One or more of the Action class's methods are executed and a String result is returned. Based on the value of the result, a specific view page (in this example that view page is HelloWorld.jsp) is rendered.

package org.apache.struts.helloworld.action;

import org.apache.struts.helloworld.model.MessageStore;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private MessageStore messageStore;
	
	public String execute() throws Exception {
		
		messageStore = new MessageStore() ;
		return SUCCESS;
	}

	public MessageStore getMessageStore() {
		return messageStore;
	}

	public void setMessageStore(MessageStore messageStore) {
		this.messageStore = messageStore;
	}
}

Step 3 - Create The View  HelloWorld.jsp
Create the below jsp in src/main/webapp for the  project.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=ISO-8859-1">
<title>Hello World!</title>
</head>
<body>
    <h2><s:property value="messageStore.message" /></h2>
</body>
</html>

Step 4 - Add The Struts Configuration In struts.xml

Edit the struts.xml file ( in the  project that file is in the src/main/resources folder) to add the action mapping.
Place the action node (action name="hello") between the opening and closing package node, just after the action mapping with the name="index". Your complete struts.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
  <constant name="struts.devMode" value="true" />
  <package name="basicstruts2" extends="struts-default">
  <action name="index">
    <result>/index.jsp</result>
  </action>
		
  <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
    <result name="success">/HelloWorld.jsp</result>
  </action>
</package>
</struts>

Step 5 - Create The URL Action
In index.jsp (see WebContent folder for Ant project and src/main/webapp for Mvn project) let's add an Action URL the user can click on to tell the Struts 2 framework to run the execute method of the HelloWorldAction class and render the HelloWorld.jsp view.
First add the taglib directive at the top of the jsp <%@ taglib prefix="s" uri="/struts-tags" %>. Next add this p tag <p><a href="<s:url action='hello'/>">Hello World</a></p> after the h1 tag. Your newindex.jsp should look like:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
</html>

The Struts url tag creates the URL with an action of hello. The hello action was mapped to the HelloWorldAction class and its execute method. When the user clicks on the above URL it will cause the Struts 2 framework to run the execute method of the HelloWorldAction class. After that method returns the String success, the view page HelloWorld.jsp will be rendered.

Step 6 - Build the WAR File and Run The Application

web容器中运行正常就成功咯

分享到:
评论

相关推荐

    Struts2入门教程-分析及讲解

    除了这个入门教程,还有许多官方文档、在线教程和社区论坛可以提供帮助,如Apache Struts官网、Stack Overflow等,遇到问题可以去这些地方寻求解答。 通过本教程的学习,你将能够理解和使用Struts2框架搭建web应用...

    struts2+hibernate+spring

    Struts2、Hibernate和Spring是Java Web开发中的三大框架,它们的整合应用,通常被称为SSH(Struts2、Spring、Hibernate)集成。这个项目展示了如何将这三个框架有效地结合在一起,实现一个功能齐全的Web应用程序,...

    Struts2+Hibernate+Spring整合后的增删改查

    Struts2、Hibernate和Spring是Java Web开发中的三大框架,它们各自负责不同的职责:Struts2主要用于控制应用程序的流程,Hibernate则专注于对象关系映射(ORM),实现了数据持久化,而Spring作为全能型框架,提供了...

    displytag 学习与参考和源文件

    和DataGrid功能一样强大. 此资源包括三个包,有官方的,可以跟着学习,不过是英文的(display tag学习);有displytag最小单元,你的工程中要添加,就把这个copy过来就可以了;还有一个是doc参考文档. blog上有相应的...

    BBS下载(毕业设计迪哥)

    JSP的优势在于可以与Java的后端框架如Spring、Struts等无缝集成,提供强大的功能和良好的可维护性。 3. **MySQL**:MySQL是一种流行的关系型数据库管理系统(RDBMS),广泛应用于Web应用中。在BBS系统中,MySQL用于...

    北风客户关系管理源码 CRM

    该项目使用流行的Struts 2 + Hibernate + Spring + JQuery整合开发。众所周知Struts 2框架比之前辈要胜出不少,而Hibernate框架也是持久化中的首选,而大名鼎鼎的Spring更是无所不能。再加上设计轻巧的JQuery框架,...

    SSH搭建 视频教程 简单 Java 框架

    SSH(Struts + Spring + Hibernate)是Java Web开发中一种常见的技术栈,它结合了Struts的MVC架构、Spring的依赖注入与事务管理以及Hibernate的对象关系映射工具,为开发者提供了一种高效且灵活的开发环境。...

    JAVA自学之路

    数据库,可以选择使用Oracle或者MySQL开始 。 JDBC,用Java连接数据库。 可以选择在这个地方切入Hibernate,也可以选择在Struts 之后。 HTML CSS JavaScript,这些东西是做web项目必需的内容 。 Servlet JSP,...

    韩顺平.SSH框架视频教程-项目实战-校内网(含源代码、设计文档、关系图和数据库脚本)

    【SSH框架】是Java开发中的一个经典组合,由Spring、Struts和Hibernate三个开源框架集成。这个"韩顺平.SSH框架视频教程-项目实战-校内网"提供了一个全面学习SSH框架实操的资源,适合Java Web开发者,尤其是初学者...

    SSH集成录像

    2. **创建项目**:在MyEclipse中创建一个新的Web项目,然后添加Struts、Hibernate和Spring的jar包到项目的类路径中。 3. **配置Struts**:编写struts-config.xml文件,定义Action和ActionForm,设置控制器的路由...

    java开发erp源码-NSFW_Systen:学SSH时写的纳税服务系统,详情可看ReadMe

    Struts2,Spring,Hibernate JSP Javascript,jQuery,AJAX 某些工具类及组件如(FileUtil,POI,DateUtils) JS组件:日期组件、富文本编辑器、Fusionchart(图表组件) 抽取Action、Service、Dao,全局异常处理 权限控制...

Global site tag (gtag.js) - Google Analytics