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

Struts-2.3.24.1官方例子-struts2-blank

2016-03-12 18:01 651 查看
一、配置文件

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

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

<!-- Restricts access to pure JSP files - access available only via Struts action -->
<security-constraint>
<display-name>No direct JSP access</display-name>
<web-resource-collection>
<web-resource-name>No-JSP</web-resource-name>
<url-pattern>*.jsp</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>no-users</role-name>
</auth-constraint>
</security-constraint>

<security-role>
<description>Don't assign users to this role</description>
<role-name>no-users</role-name>
</security-role>

</web-app>


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

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />

<package name="default" namespace="/" extends="struts-default">

<default-action-ref name="index" />

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

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

<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>

<include file="example.xml"/>

<!-- Add packages here -->

</struts>


3.example.xml

<?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>
<package name="example" namespace="/example" extends="default">

<action name="HelloWorld" class="example.HelloWorld">
<result>/WEB-INF/jsp/example/HelloWorld.jsp</result>
</action>

<action name="Login_*" method="{1}" class="example.Login">
<result name="input">/WEB-INF/jsp/example/Login.jsp</result>
<result type="redirectAction">Menu</result>
</action>

<action name="*" class="example.ExampleSupport">
<result>/WEB-INF/jsp/example/{1}.jsp</result>
</action>

<!-- Add actions here -->
</package>
</struts>


4.

二、action

1.

/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package example;

import com.opensymphony.xwork2.ActionSupport;

/**
* Base Action class for the Tutorial package.
*/
public class ExampleSupport extends ActionSupport {
}


2.

/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package example;

import java.util.Arrays;
import java.util.List;

/**
* <code>Set welcome message.</code>
*/
public class HelloWorld extends ExampleSupport {

public String execute() throws Exception {
setMessage(getText(MESSAGE));
return SUCCESS;
}

/**
* Provide default valuie for Message property.
*/
public static final String MESSAGE = "HelloWorld.message";

/**
* Field for Message property.
*/
private String message;

/**
* Return Message property.
*
* @return Message property
*/
public String getMessage() {
return message;
}

/**
* Set Message property.
*
* @param message Text to display on HelloWorld page.
*/
public void setMessage(String message) {
this.message = message;
}

public List<YesNo> getValues() {
//        return null;
return Arrays.asList(YesNo.values());
}
}


3.

/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package example;

import org.apache.struts2.interceptor.validation.SkipValidation;

public class Login extends ExampleSupport {

@Override
public String execute() throws Exception {
return SUCCESS;
}

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

}


4.

package example;

public enum YesNo {
YES, NO, middle
}


三、JSP

1.HelloWorld.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title><s:text name="HelloWorld.message"/></title>
</head>

<body>
<h2><s:property value="message"/></h2>

<h3>Languages</h3>
<ul>
<li>
<s:url id="url" action="HelloWorld">
<s:param name="request_locale">en</s:param>
</s:url>
<s:a class="test" href="%{url}">English</s:a>
</li>
<li>
<s:url id="url" action="HelloWorld">
<s:param name="request_locale">es</s:param>
</s:url>
<s:a href="%{url}">Espanol</s:a>
</li>
</ul>

<!-- 在package.properties配置信息 -->
<s:checkboxlist name="test" list="values" listLabelKey="'test-' + name().toLowerCase()" />

</body>
</html>


2.Login.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Sign On</title>
</head>

<body>
<s:form action="Login">
<s:textfield key="username"/>
<s:password key="password" />
<s:submit/>
</s:form>
</body>
</html>


3.Menu.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:include value="Missing.jsp"/>


4.Missing.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Missing Feature</title></head>

<body>
<p>
<s:text name="Missing.message"/>
</p>
</body>
</html>


5.Register.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:include value="Missing.jsp"/>


6.Welcome.jsp

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Welcome</title>
<link href="<s:url value="/css/examplecss"/>" rel="stylesheet"
type="text/css"/>
</head>

<body>
<h3>Commands</h3>
<ul>
<li><a href="<s:url action="Login_input"/>">Sign On</a></li>
<li><a href="<s:url action="Register"/>">Register</a></li>
</ul>

</body>
</html>


7.

四、资源文件等

1.package.properies

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.
test-yes=Yo
test-no=Nein
test-middle=mod


2.Login-validation.xml

<!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">
<field-validator type="requiredstring">
<message key="requiredstring"/>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message key="requiredstring"/>
</field-validator>
</field>
</validators>


3.velocity.properies

runtime.log.logsystem.class=org.apache.velocity.runtime.log.NullLogChute
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: