标签:
一、配置文件
1.web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 5 6 <display-name>Struts Blank</display-name> 7 8 <filter> 9 <filter-name>struts2</filter-name> 10 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 11 </filter> 12 13 <filter-mapping> 14 <filter-name>struts2</filter-name> 15 <url-pattern>/*</url-pattern> 16 </filter-mapping> 17 18 <welcome-file-list> 19 <welcome-file>index.html</welcome-file> 20 </welcome-file-list> 21 22 <!-- Restricts access to pure JSP files - access available only via Struts action --> 23 <security-constraint> 24 <display-name>No direct JSP access</display-name> 25 <web-resource-collection> 26 <web-resource-name>No-JSP</web-resource-name> 27 <url-pattern>*.jsp</url-pattern> 28 </web-resource-collection> 29 <auth-constraint> 30 <role-name>no-users</role-name> 31 </auth-constraint> 32 </security-constraint> 33 34 <security-role> 35 <description>Don‘t assign users to this role</description> 36 <role-name>no-users</role-name> 37 </security-role> 38 39 </web-app>
2.struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 9 <constant name="struts.devMode" value="true" /> 10 11 <package name="default" namespace="/" extends="struts-default"> 12 13 <default-action-ref name="index" /> 14 15 <global-results> 16 <result name="error">/WEB-INF/jsp/error.jsp</result> 17 </global-results> 18 19 <global-exception-mappings> 20 <exception-mapping exception="java.lang.Exception" result="error"/> 21 </global-exception-mappings> 22 23 <action name="index"> 24 <result type="redirectAction"> 25 <param name="actionName">HelloWorld</param> 26 <param name="namespace">/example</param> 27 </result> 28 </action> 29 </package> 30 31 <include file="example.xml"/> 32 33 <!-- Add packages here --> 34 35 </struts>
3.example.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <package name="example" namespace="/example" extends="default"> 8 9 <action name="HelloWorld" class="example.HelloWorld"> 10 <result>/WEB-INF/jsp/example/HelloWorld.jsp</result> 11 </action> 12 13 <action name="Login_*" method="{1}" class="example.Login"> 14 <result name="input">/WEB-INF/jsp/example/Login.jsp</result> 15 <result type="redirectAction">Menu</result> 16 </action> 17 18 <action name="*" class="example.ExampleSupport"> 19 <result>/WEB-INF/jsp/example/{1}.jsp</result> 20 </action> 21 22 <!-- Add actions here --> 23 </package> 24 </struts>
4.
二、action
1.
1 /* 2 * $Id$ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 package example; 23 24 import com.opensymphony.xwork2.ActionSupport; 25 26 /** 27 * Base Action class for the Tutorial package. 28 */ 29 public class ExampleSupport extends ActionSupport { 30 }
2.
1 /* 2 * $Id$ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 package example; 23 24 import java.util.Arrays; 25 import java.util.List; 26 27 /** 28 * <code>Set welcome message.</code> 29 */ 30 public class HelloWorld extends ExampleSupport { 31 32 public String execute() throws Exception { 33 setMessage(getText(MESSAGE)); 34 return SUCCESS; 35 } 36 37 /** 38 * Provide default valuie for Message property. 39 */ 40 public static final String MESSAGE = "HelloWorld.message"; 41 42 /** 43 * Field for Message property. 44 */ 45 private String message; 46 47 /** 48 * Return Message property. 49 * 50 * @return Message property 51 */ 52 public String getMessage() { 53 return message; 54 } 55 56 /** 57 * Set Message property. 58 * 59 * @param message Text to display on HelloWorld page. 60 */ 61 public void setMessage(String message) { 62 this.message = message; 63 } 64 65 public List<YesNo> getValues() { 66 // return null; 67 return Arrays.asList(YesNo.values()); 68 } 69 }
3.
1 /* 2 * $Id$ 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 package example; 23 24 import org.apache.struts2.interceptor.validation.SkipValidation; 25 26 public class Login extends ExampleSupport { 27 28 @Override 29 public String execute() throws Exception { 30 return SUCCESS; 31 } 32 33 @SkipValidation 34 public String form() throws Exception { 35 return INPUT; 36 } 37 38 private String username; 39 40 public String getUsername() { 41 return username; 42 } 43 44 public void setUsername(String username) { 45 this.username = username; 46 } 47 48 private String password; 49 50 public String getPassword() { 51 return password; 52 } 53 54 public void setPassword(String password) { 55 this.password = password; 56 } 57 58 }
4.
1 package example; 2 3 public enum YesNo { 4 YES, NO, middle 5 }
三、JSP
1.HelloWorld.jsp
1 <%@ page contentType="text/html; charset=UTF-8" %> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <head> 5 <title><s:text name="HelloWorld.message"/></title> 6 </head> 7 8 <body> 9 <h2><s:property value="message"/></h2> 10 11 <h3>Languages</h3> 12 <ul> 13 <li> 14 <s:url id="url" action="HelloWorld"> 15 <s:param name="request_locale">en</s:param> 16 </s:url> 17 <s:a class="test" href="%{url}">English</s:a> 18 </li> 19 <li> 20 <s:url id="url" action="HelloWorld"> 21 <s:param name="request_locale">es</s:param> 22 </s:url> 23 <s:a href="%{url}">Espanol</s:a> 24 </li> 25 </ul> 26 27 <!-- 在package.properties配置信息 --> 28 <s:checkboxlist name="test" list="values" listLabelKey="‘test-‘ + name().toLowerCase()" /> 29 30 </body> 31 </html>
2.Login.jsp
1 <%@ page contentType="text/html; charset=UTF-8" %> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <head> 5 <title>Sign On</title> 6 </head> 7 8 <body> 9 <s:form action="Login"> 10 <s:textfield key="username"/> 11 <s:password key="password" /> 12 <s:submit/> 13 </s:form> 14 </body> 15 </html>
3.Menu.jsp
1 <%@ page contentType="text/html; charset=UTF-8" %> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <s:include value="Missing.jsp"/>
4.Missing.jsp
1 <%@ page contentType="text/html; charset=UTF-8" %> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <head><title>Missing Feature</title></head> 5 6 <body> 7 <p> 8 <s:text name="Missing.message"/> 9 </p> 10 </body> 11 </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
1 <%@ page contentType="text/html; charset=UTF-8" %> 2 <%@ taglib prefix="s" uri="/struts-tags" %> 3 <html> 4 <head> 5 <title>Welcome</title> 6 <link href="<s:url value="/css/examplecss"/>" rel="stylesheet" 7 type="text/css"/> 8 </head> 9 10 <body> 11 <h3>Commands</h3> 12 <ul> 13 <li><a href="<s:url action="Login_input"/>">Sign On</a></li> 14 <li><a href="<s:url action="Register"/>">Register</a></li> 15 </ul> 16 17 </body> 18 </html>
7.
四、资源文件等
1.package.properies
1 HelloWorld.message= Struts is up and running ... 2 requiredstring = ${getText(fieldName)} is required. 3 password = Password 4 username = User Name 5 Missing.message = This feature is under construction. Please try again in the next interation. 6 test-yes=Yo 7 test-no=Nein 8 test-middle=mod
2.Login-validation.xml
1 <!DOCTYPE validators PUBLIC 2 "-//Apache Struts//XWork Validator 1.0.2//EN" 3 "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> 4 5 <validators> 6 <field name="username"> 7 <field-validator type="requiredstring"> 8 <message key="requiredstring"/> 9 </field-validator> 10 </field> 11 <field name="password"> 12 <field-validator type="requiredstring"> 13 <message key="requiredstring"/> 14 </field-validator> 15 </field> 16 </validators>
3.velocity.properies
1 runtime.log.logsystem.class=org.apache.velocity.runtime.log.NullLogChute
Struts-2.3.24.1官方例子-struts2-blank
标签:
原文地址:http://www.cnblogs.com/shamgod/p/5269544.html