struts2入门实例

struts2是javaee开发中一款优秀的mvc框架,他并不是从struts1升级而来,而是xwork框架演变而来。struts2可以单独使用,也可以和spirng集成,做spring开发web项目的mvc框架。这里介绍入门struts2示例。

1、新建maven web项目,引入struts2-core-2.5.16依赖,并配置jetty-maven-plugin,这里用jetty启动项目。

<dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>${struts2.version}</version>
    </dependency>

jetty-maven-plugin插件配置:

<build>
    <finalName>struts2</finalName>
    <plugins>
        <plugin>
              <groupId>org.eclipse.jetty</groupId>
              <artifactId>jetty-maven-plugin</artifactId>
              <version>9.4.7.v20170914</version>
              <configuration>
                  <webApp>
                      <contextPath>/${build.finalName}</contextPath>
                  </webApp>
                  <stopKey>CTRL+C</stopKey>
                  <stopPort>8999</stopPort>
                  <scanIntervalSeconds>10</scanIntervalSeconds>
                  <scanTargets>
                      <scanTarget>src/main/webapp/WEB-INF/webapp.xml</scanTarget>
                  </scanTargets>
              </configuration>
        </plugin>
    </plugins>
  </build>

2、配置web.xml,引入struts2请求过滤器。

struts2没有2.4版本,现在最高版本是2.5.16,在2.3版本过滤器全称:org.apache.struts2.dispatcher.ng.filter.

StrutsPrepareAndExecuteFilter,而在2.5版本中改为:org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter。中间已经去掉了ng包名。这里需要注意一下。

<welcome-file-list>
  <welcome-file>index</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>

3、编写action类,并覆盖execute方法。

package com.xxx.struts.action;
import com.opensymphony.xwork2.ActionSupport;
import com.xxx.struts.domain.MessageStore;
public class UserAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private MessageStore messageStore;
	@Override
	public String execute() throws Exception {
		messageStore = new MessageStore("struts2 is a mvc framework");
		return SUCCESS;
	}	
	public MessageStore getMessageStore(){
		return this.messageStore;
	}
}

这里辅助类MessageStore的代码:

package com.xxx.struts.domain;
import java.io.Serializable;
public class MessageStore implements Serializable {
	private static final long serialVersionUID = 1L;
	private String message;
	public String getMessage(){
		return message;
	}
	public MessageStore(String message){
		this.message = message;
	}
}

4、配置struts.xml。

<struts>
    <constant name="struts.devMode" value="true"/>
    <package name="struts2" extends="struts-default">
        <action name="index">
            <result>/index.jsp</result>
        </action>
        <action name="user" class="com.xxx.struts.action.UserAction">
            <result name="success">/user.jsp</result>
        </action>
    </package>
</struts>

这里配置了名为struts2的package,其中有一个默认的action名为index,通过/struts2/index访问,默认后面去掉了".action"后缀。另外配置了一个名为user的action,通过地址/struts/user.action来访问。

这里配置的示例是启动项目访问index.jsp,然后点击/user.action链接跳转到user.jsp。

5、配置简单index.jsp,user.jsp页面。他们是同级位置,均在src/main/webapp目录下。

index.jsp

<!doctype html>
<%@page language="java" contentType="text/html;charset=UTF-8" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
 <title>index.page</title>
</head>
<body>
<h2>hello,struts2,this is my first page.<a href="<s:url action='user'/>"/>user</a></h2>
</body>
</html>

页面中引入了struts2的标签,通过<s:url action="user"/>,就指定了user.action,而user.action对应的Action为struts.xml文件中配置的com.xxx.struts.action.UserAction,而访问user.action的结果在<result name="success">/user.jsp</result>中指定了,这样一个模型视图控制器的框架就体现出来了。

user.jsp

<!doctype html>
<%@page language="java" contentType="text/html;charset=UTF-8" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
 <title>user.page</title>
</head>
<body>
<h2>hello,struts2,<s:property value="messageStore.message"/></h2>
</body>
</html>

6、运行mvn jetty:run启动服务,访问http://localhost:8080/struts2/index,这里默认省略了index后面的".action"。

从页面元素中生成的user的链接,可以看到是/struts2/user.action.

这里介绍的示例程序是一个struts2入门的基础,可以看到,基本具备了mvc的功能。对于初次接触struts2的人来说,我们经常遇到下列类似问题:

  • There is no Action mapped for namespace [/] and action name [] associated with context path [/struts2].
  • No result defined for action com.xxx.struts.action.UserAction and result success
  • HTTP ERROR 404 Problem accessing /struts2/WEB-INF/content/user.jsp. Reason

除了平时的积累,我们是可以有一些办法的。这里介绍一个struts2插件工具struts2-config-browser-plugin,他可以帮助我们查看我们写的action和配置的struts.xml是否正确。

只需要在pom.xml配置文件中引入如下配置即可:

<dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-config-browser-plugin</artifactId>
      <version>${struts2.version}</version>
  </dependency>

加入之后,保存pom.xml文件,等待jar包下载完成即可,无需任何配置,直接启动项目。访问地址是:http://localhost:8080/struts2/config-browser/actionNames.action如下:

在这个界面可以看到,一个默认的index,一个我们自定义的user的action。

点击user,我们可以看到关于user.action的详细信息:访问的路径,访问result页面,result页面的位置。

这样,针对上面出现的三个问题,就很好排除了:

  1. 如果这个配置查看页面没有出现user这个action,那么访问/struts/user.action就会出现第一个问题,There is no Action mapped for namespace [/] and action name [user] associated with context path [/struts2]。
  2. 在user的action页面,如果Results列表为空,表明没有定义结果页面,我们需要检查struts.xml中action->result的配置。
  3. 如果Results列表有值,访问还出现404,那么检查user.jsp的位置是否在我们定义路径下。/user.jsp表明在src/main/webapp目录下。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值