您的位置:首页 > 编程语言 > Java开发

Struts2

2016-06-04 17:39 585 查看

Struts2简单例子

目录结构



所需jar包



pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.lee</groupId>
<artifactId>struts2-blank</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>struts2-blank</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24.3</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<finalName>struts2-blank</finalName>

<!-- jetty服务器插件 -->
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.8.v20150217</version>
</plugin>
</plugins>
</build>
</project>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<display-name>Struts-Blank</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.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>

</web-app>


struts.xml

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

<!-- 将action托管给spring
需要struts2-spring-plugin-*.jar
<constant name="struts.objectFactory" value="spring"></constant>
-->

<!-- 所有匹配*.action的请求都由struts2处理 -->
<constant name="struts.action.extension" value="action" />
<!-- 是否启用开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- struts配置文件改动后,是否重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 设置浏览器是否缓存静态内容 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
<constant name="struts.i18n.reload" value="true" />
<!-- 文件上传最大值 -->
<constant name="struts.multipart.maxSize" value="104857600" />
<!-- 让struts2支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- Action名称中是否还是用斜线 -->
<constant name="struts.enable.SlashesInActionNames" value="false" />
<!-- 允许标签中使用表达式语法 -->
<constant name="struts.tag.altSyntax" value="true" />
<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
<constant name="struts.dispatcher.parametersWorkaround" value="false" />

<package name="user" namespace="/User" extends="struts-default">
<action name="Login">
<result>/login.jsp</result>
</action>
<action name="Welcome" class="com.lee.user.action.WelcomeUserAction">
<result name="SUCCESS">/welcome_user.jsp</result>
</action>

<!-- Action中支持通配符
该支持Login_execute, Login_test 等Action名称
即该Action类中含有名为{1}的方法名,
上面即为Login_excute(默认方法)
不同方法返回不同值<result name="*">*.jsp</result>显示不同页面
<action name="Login_*" method="{1}" class="com.lee.user.action.WelcomeUserAction">
<result name="input">/example/Login.jsp</result>
<result type="redirectAction">Menu</result>
</action>
-->
</package>
</struts>


login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>Struts 2 Hello World Example</h1>

<s:form action="Welcome"><!-- 根目录下 -->
<s:textfield name="username" label="Username" />
<s:password name="password" label="Password" />
<s:submit />
</s:form>

</body>
</html>


welcome_user.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<h1>Struts 2 Hello World 示例</h1>

<h2>
Hello
<s:property value="username" />
</h2>

</body>
</html>


WelcomeUserAction.java

package com.lee.user.action;

/*
*在Struts2中,Action类实现任何接口或扩展任何类不是必需的,
*但它需要创建一个execute()方法来实现所有的业务逻辑,
*并返回一个字符串值,告诉用户重定向到哪里。
*一些用户实现 com.opensymphony.xwork2.Action 类,
*但它是完全可选的(不是必须的),
*Struts 2的Action类是可选的,
*但是仍然允许执行com.opensymphony.xwork2.Action
*的一些方便的常量,或者扩展
*com.opensymphony.xwork2.ActionSupport
*对于一些常见的默认动作执行的功能。
*/
public class WelcomeUserAction {

private String username;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

// all struts logic here
public String execute() {

return "SUCCESS";

}
}


URL

在此我使用jetty服务器

mvn jetty:run


http://localhost:8080/User/Login.action


Struts2 修改 struts.xml 路径

默认路径

struts2.X配置文件默认存放路径在/WEB-INF/classes目录下,即将struts.xml放在src的目录下。

首先要明白struts2加载配置文件都是从自己的jar包和/WEB-INF/classes两个默认的位置加载的。

若修改struts2.x配置文件的存放位置,在web.xml配置过虑器时,具体配置如下:

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>
struts-default.xml,
struts-plugin.xml,
struts2/struts.xml
</param-value>
</init-param>
</filter>


注意点

若设置了config参数,那struts-default.xml等原来struts2默认加载的文件也要手动指定,否则不会自动加载。

struts-plugin.xml也需要指定。因为在struts2使用2.1.6版本时:

若需要和spring集成的话,struts2-spring-plugin-2.1.6.jar中有struts-plugin.xml这个文件。

若struts2要支持json的话, json-plugin-0.34.jar中也有一个叫struts-plugin.xm的文件。

采用相对/WEB-INF/classes的相对路径。本例放在了/WEB-INF/classes/struts目录下。当然也可以写成classpath:struts2/struts.xml

若不在这里配置struts-default.xml,struts-plugin.xml,也可以在struts.xml文件中添加include标签将两个文件包括进去。

<include file="struts-default.xml" />
<include file="struts-plugin.xml" />


使用标签添加其他子配置文件时,file属性也要是一个相对/WEB-INF/classes的路径。

若子配置文件路径是/WEB-INF/classes/configs/struts/student/struts-config.xml的话,

file属性值应该写configs/struts/student/struts-config.xml。

若有多个子配置文件可以采用扫描的方式

可能遇到的问题:

警告: Could not find action or result

There is no Action mapped for namespace / and action name hello. - [unknown location]

为什么指定了自己的struts.xml文件路径依然访问不到呢?

原因依然在struts加载配置文件的方式,struts并不是获取的配置文件相对应用(项目)的路径,而是相对src,对于web是相对/WEB-INF/classes文件夹的路径,现在知道了最终的解决方案了?

对了,就是把web.xml中的[/WEB-INF/struts.xml]改成 [../struts.xml],即使用相对/WEB-INF/classes文件夹的路径!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: