struts2.3.16之环境搭建

时间:2022-06-23 04:59:29

    1、首先先在网上下载struts2.3.16的完整版,里面有几个文件夹,分别为:apps——范例代码,docs——api,lib——类库,src——源码

    2、在myeclipse中新建一个java web project,名称为sshTest

    3、添加所需要的类包,在WebContent/WEB-INF/lib目录下,以下为必需的类包

    struts2.3.16之环境搭建

    4、接下来就是编写struts配置文件,struts配置文件放在src目录下

<?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>
<!-- bean type="com.opensymphony.xwork2.ObjectFactory" name="spring" class="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.objectFactory" value="spring" /-->
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="true" />

<package name="example" namespace="/" extends="struts-default">
<default-action-ref name="index" />

<global-results>
<result name="error">/error.jsp</result>
</global-results>

<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error" />
</global-exception-mappings>

<action name="Login" class="example.LoginAction"
method="execute">
<result>/example/Welcome.jsp</result>
<result name="turntofirst" type="redirectAction">First</result>
</action>

<action name="First" class="example.FirstAction" method="execute">
<result name="success">/example/first.jsp</result>
</action>
</package>

<!-- include file="example.xml"/-->
</struts>
    5、然后是编写web.xml配置文件来启动struts框架,配置struts2的核心过滤器

<?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>SSH test</display-name>

<!-- listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener-->

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

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>
    6、部署项目到tomcat中,如果后台报错,

Line: 209 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:209:-1

原因两个:
1.lib中多导入包的大原因:去掉struts2-spring-plugin-2.1.8包即可,因为没有用到spring。
2.还有的原因是用spring了,却没加监听器,在web.xml里面加上

 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext*.xml</param-value>
 </context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


    7、项目结构为

struts2.3.16之环境搭建



    进行测试:

1、配置2个action,其中result中type为redirectAction,转向另一个action

<action name="Login" class="example.LoginAction"
method="execute">
<result>/example/Welcome.jsp</result>
<result name="turntofirst" type="redirectAction">First</result>
</action>

<action name="First" class="example.FirstAction" method="execute">
<result name="success">/example/first.jsp</result>
</action>
2、在src下编写action文件,并加入相应的属性、getter/setter、execute方法

LoginAction:

public String execute() throws Exception {
if("kevin".equals(getUsername())){
return SUCCESS;
}else{
return "turntofirst";
}
}

@SkipValidation
public String form() throws Exception {
return INPUT;
}

private String username;

public String getUsername() {
return username;
}

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

private String password;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

FirstAction:

private String msg;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public String execute() {
msg = "第一个struts程序";
return "success";
}

3、访问页面http://localhost:8080/sshTest/example/Login.jsp

struts2.3.16之环境搭建

如果username为kevin则转向welcome.jsp页面,否则转向first.jsp页面

first.jsp页面用el表达式输出msg

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>first struts</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
${msg}
</body>
</html>

这样,一个简单的struts程序就搭建起来了


接下来我们添加个登录校验

1、首先需要我们的action继承ActionSupport类,需要重写其validate()方法

2、在LoginAction中添加validate()方法

@Override
// 添加校验
public void validate() {
if (getUsername() == null || getUsername().trim().equals("")) {
addFieldError("username", "用户名不能为空!");
}
if (getPassword() == null || getPassword().trim().equals("")) {
addFieldError("password", "密码不能为空!");
}
}
3、struts.xml中result配置input

<result name="input">/demo/jsp/Login.jsp</result>

4、当页面中用户名或密码为空时,struts框架就会为我们自动校验

struts2.3.16之环境搭建


但这种校验方式不能支持国际化,所以我们可以采用struts2的校验框架
下面是校验规则的定义文件:

<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">

<validators>
<field name="username">
<!-- requiredstring类型表示该表单域必须填写 -->
<field-validator type="requiredstring">
<!-- 如果校验失败则显示以下信息 -->
<message key="requiredstring" />
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message key="requiredstring" />
</field-validator>
</field>
</validators>
文件命名为:类名--validation.xml,在这里则为LoginAction-validation.xml,放在和action同一路径下,同样要定义input的逻辑视图名
定义一个属性文件:package.properties,校验信息从这里获取
HelloWorld.message= Struts is up and running ...
requiredstring = ${getText(fieldName)} is required.
password = Password
username = User Name
Missing.message = This feature is under construction. Please try again in the next interation.