1.Hello World Using Struts 2

本系列文章均为自我学习而作,禁止转载


We use JSP,STRUTS2 to to create a "Hello World" example.

you need to do four things:

  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

我们需要2张jsp网页, 2个java文件,以及一个struts.xml文件来制作一份经典的struts2的helloworld message。

首先是index.jsp 此网页包含了:

<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>

给一个hello.action形式的拦截性链接给服务器来获取新的一张jsp网页(helloworld.jsp)

当然我们既然要用struts2进行拦截操作。首先要做的是(导包,这个不谈。。blank自己拷),在web.xml中设置拦截器filter

web.xml(filter和mapping)

    <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>
设置拦截器后,struts便会拦截一切请求数据了。

接下来。既然网页配置完毕了。就先操作struts.xml配置文件了。

struts.xml配置基本以constant类。package类。include类为主。

现阶段用到的是constant(基本就是抄)

package类(主要配置)include pass。

源码:

<?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>
constant中struts.devMode=developmentMode开发模式,value is boolean,选是就是可以在不重启类似tomcat,jboss等运行环境的情况下,修改struts.xml并生效

package就是我们需要配置的东西。

name=“自定义名”(这中间少了一个namespace="地址路径"这里可以写成 namespace="/")extends 因为它是第一package就需要继承的struts的default

<action>这块就是我们需要写的跳转链接了。此处的name对应着index.action

这里扯一个题外话(其实后缀名action是可以修改的)在constant类下有一个name为struts.action.extension的参数。他的value对应着后缀名 eg:value="action,do"

这样就可以拦截  .do和.action的文件了。

扯回来。。。

就是说我们输入localhost:8080/项目名/index.action就可以跳转到到index.jsp(result中的值)

这是第一种不处理直接跳转的方式。

第二个action才是我们需要跳转的:他写的很清楚 name=hello就是说我们的index.jsp中的s:url action="hello" 当我们点击那个时,就会跳转到此拦截器了。

class对应着对应的java类。method代表其中的方法体

(再扯出去)method其实有2种方式一种是填写method=“execute”这样的形式来调用方法体。(其实默认就死execute,题目也就是为了让这个method属性出现而已。。)

因为struts2从webwork中生,所以也可以使用它的方式就是 s:url action="hello!execute"就不用在method中写了。

扯回来。。

result name=success就是说他的返回值是success的话就跳转helloworld。jsp(就是个写了<h1> Hello World!的jsp而已。。不贴了 )

接下来就是2个java文件了。

一个已经介绍过了就是struts.xml中填写的class类org.apache.struts.helloworld.action.HelloWorldAction。

此类是一个action用来处理请求响应接收参数的(本题中无参数。)

他有个非强制要求,继承actionsupport类或者action接口

action:

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;
	}

}
execute就是我们需要实现的方法
MessageStore:(javaBean)

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;
	}

}
helloworld.jsp

<h2><s:property value="messageStore.message" /></h2>

这样,一个普通的helloworld就完成了。大家试试吧




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值