先按照文档,做一次:
1,建立WEB.XML:
代码
-
- <!---->xml version="1.0" encoding="UTF-8"?>
- <web-app>
-
- <display-name>Struts Blankdisplay-name>
-
- <filter>
- <filter-name>struts2filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcherfilter-class>
- filter>
-
- <filter-mapping>
- <filter-name>struts2filter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
- listener>
-
- <welcome-file-list>
- <welcome-file>index.htmlwelcome-file>
- welcome-file-list>
- web-app>
文档中有一个org.apache.struts2.dispatcher.FilterDispatcher的filter
还有org.springframework.web.context.ContextLoaderListener
2.建SPRING的BEAN配置文件:applicationContext.xml
代码
- <!---->xml version="1.0" encoding="UTF-8"?>
- <!---->>
-
- <beans default-autowire="autodetect">
-
- beans>
3.建struts配置文件:
代码
- <!---->
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
-
- <struts>
-
- <include file="example.xml"/>
-
-
-
- struts>
演示了一下配置文件分块的方法,下面这个才起作用:
<!---->
"-//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="struts-default">
-
- <action name="HelloWorld" class="example.HelloWorld">
- <result>/example/HelloWorld.jspresult>
- action>
-
- <action name="Login!*" method="{1}" class="example.Login">
- <result name="input">/example/Login.jspresult>
- <result type="redirect-action">Menuresult>
- action>
-
- <action name="*" class="example.ExampleSupport">
- <result>/example/{1}.jspresult>
- action>
-
-
- package>
- struts>
4,录入验证:Login-validation.xml
这个很象struts1.x
代码
- <!---->
- "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
- "http://www.opensymphony.com/xwork/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>
5.建立资源文件package.properties,这个也很象struts1.x
代码
- 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.
6.搞个action类,和WW2一模一样:
代码
- package example;
-
- public class Login extends ExampleSupport {
-
- public String execute() throws Exception {
-
- if (isInvalid(getUsername())) return INPUT;
-
- if (isInvalid(getPassword())) return INPUT;
-
- return SUCCESS;
- }
-
- private boolean isInvalid(String value) {
- return (value == null || value.length() == 0);
- }
-
- 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;
- }
-
- }
7.搞个helloworld类,也和WW2一样:
这里调用了资源文件中的 message, 这里比struts1.X类似功能要直接,会好用一些
代码
- package example;
-
- /**
- * <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;
- }
- }
8。搞个父类出来,这里只是演示用,所以空的
ExampleSupport.java
代码
- package example;
-
- import com.opensymphony.xwork2.ActionSupport;
-
-
-
-
- public class ExampleSupport extends ActionSupport {
- }
9。可以测试一下了:
代码
- package example;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.config.entities.ActionConfig;
-
- import java.util.Map;
- import java.util.Collection;
- import java.util.List;
-
- public class LoginTest extends ConfigTest {
-
- public void FIXME_testLoginConfig() throws Exception {
- ActionConfig config = assertClass("example", "Login!input", "example.Login");
- assertResult(config, ActionSupport.SUCCESS, "Menu");
- assertResult(config, ActionSupport.INPUT, "/example/Login.jsp");
- }
-
- public void testLoginSubmit() throws Exception {
- Login login = new Login();
- login.setUsername("username");
- login.setPassword("password");
- String result = login.execute();
- assertSuccess(result);
- }
-
-
- public void FIXME_testLoginSubmitInput() throws Exception {
- Login login = new Login();
- String result = login.execute();
- assertInput(result);
- Map errors = assertFieldErrors(login);
- assertFieldError(errors,"username","Username is required.");
- assertFieldError(errors,"password","Password is required.");
- }
-
- }
代码
- package example;
-
- import com.opensymphony.xwork2.ActionSupport;
- import junit.framework.TestCase;
-
- public class HelloWorldTest extends TestCase {
-
- public void testHelloWorld() throws Exception {
- HelloWorld hello_world = new HelloWorld();
- String result = hello_world.execute();
- assertTrue("Expected a success result!",
- ActionSupport.SUCCESS.equals(result));
- assertTrue("Expected the default message!",
- hello_world.getText(HelloWorld.MESSAGE).equals(hello_world.getMessage()));
- }
- }
代码
- package example;
-
- import com.opensymphony.xwork2.ActionSupport;
- import com.opensymphony.xwork2.config.RuntimeConfiguration;
- import com.opensymphony.xwork2.config.entities.ActionConfig;
- import com.opensymphony.xwork2.config.entities.ResultConfig;
- import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
-
- import java.util.Map;
- import java.util.List;
-
- import org.apache.struts2.StrutsTestCase;
-
- public class ConfigTest extends StrutsTestCase {
-
- protected void assertSuccess(String result) throws Exception {
- assertTrue("Expected a success result!",
- ActionSupport.SUCCESS.equals(result));
- }
-
- protected void assertInput(String result) throws Exception {
- assertTrue("Expected an input result!",
- ActionSupport.INPUT.equals(result));
- }
-
- protected Map assertFieldErrors(ActionSupport action) throws Exception {
- assertTrue(action.hasFieldErrors());
- return action.getFieldErrors();
- }
-
- protected void assertFieldError(Map field_errors, String field_name, String error_message) {
-
- List errors = (List) field_errors.get(field_name);
- assertNotNull("Expected errors for " + field_name, errors);
- assertTrue("Expected errors for " + field_name, errors.size()>0);
-
- assertEquals(error_message,errors.get(0));
-
- }
-
- protected void setUp() throws Exception {
- super.setUp();
- XmlConfigurationProvider c = new XmlConfigurationProvider("struts.xml");
- configurationManager.addConfigurationProvider(c);
- configurationManager.reload();
- }
-
- protected ActionConfig assertClass(String namespace, String action_name, String class_name) {
- RuntimeConfiguration configuration = configurationManager.getConfiguration().getRuntimeConfiguration();
- ActionConfig config = configuration.getActionConfig(namespace, action_name);
- assertNotNull("Mssing action", config);
- assertTrue("Wrong class name: [" + config.getClassName() + "]",
- class_name.equals(config.getClassName()));
- return config;
- }
-
- protected ActionConfig assertClass(String action_name, String class_name) {
- return assertClass("", action_name, class_name);
- }
-
- protected void assertResult(ActionConfig config, String result_name, String result_value) {
- Map results = config.getResults();
- ResultConfig result = (ResultConfig) results.get(result_name);
- Map params = result.getParams();
- String value = (String) params.get("actionName");
- if (value == null)
- value = (String) params.get("location");
- assertTrue("Wrong result value: [" + value + "]",
- result_value.equals(value));
- }
-
- public void testConfig() throws Exception {
- assertNotNull(configurationManager);
- }
-
- }
哦,测试通过!
10。看看表示层是如何用的?
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>Languagesh3>
- <ul>
- <li>
- <s:url id="url" action="HelloWorld">
- <s:param name="request_locale">ens:param>
- s:url>
- <s:a href="%{url}">Englishs:a>
- li>
- <li>
- <s:url id="url" action="HelloWorld">
- <s:param name="request_locale">ess:param>
- s:url>
- <s:a href="%{url}">Espanols:a>
- li>
- ul>
-
- body>
- html>
Login.jsp
代码
- <%@ page contentType="text/html; charset=UTF-8" %>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <html>
- <head>
- <title>Sign Ontitle>
- head>
-
- <body>
- <s:form action="Login">
- <s:textfield label="%{getText('username')}" name="username"/>
- <s:password label="%{getText('password')}" name="password" />
- <s:submit/>
- s:form>
- body>
- html>
分享到:
相关推荐
在"初识[Struts2.0]并[使用Struts2.0]进行SSH集成"的学习过程中,"userLogin"可能是一个示例应用,涉及用户登录功能。用户登录通常包括用户输入验证、数据库查询等操作,这些可以通过Struts2.0的Action实现,而...
"struts2.0中文帮助手册.chm"是Struts 2的中文版帮助文档,对于中文使用者来说,这是一个非常实用的资源,因为它能帮助理解复杂的框架概念和用法,避免语言障碍。 "精通Struts技术.doc"和"Struts架构指导.doc"可能...
Struts2.0是Java Web开发中的一个框架,它基于MVC(Model-View-Controller)设计模式,为开发者提供了一种结构化和可扩展的方式来构建应用程序。在本手册中,我们将深入探讨Struts2.0的核心概念和使用方法。 **1. ...
初识struts2配置文件 **(1).web.xml文件** - **StrutsPrepareAndExecuteFilter**:这是struts2框架的入口点,负责初始化整个框架并处理所有HTTP请求。 - **初始化参数**:配置StrutsPrepareAndExecuteFilter时...
12.1.2 Struts 2.0与Struts 1.x框架的区别 215 12.2 MyEclipse创建基于Struts 2.0框架的项目 215 12.2.1 下载Struts 2.0开发包 216 12.2.2 创建Struts 2.0项目 216 12.2.3 配置Struts 2.0项目 217 12.2.4 创建Struts...
#### 二、初识struts2配置文件 struts2的配置主要包括以下几个文件: **(1)web.xml文件** - **StrutsPrepareAndExecuteFilter**:负责初始化整个struts框架,并处理所有请求。可以设置初始化参数,如指定加载...
在本章中,我们将深入探讨Java Web整合开发的第四章——"初识Java EE展示层组件JSP2.0"。这一章节主要关注如何利用JavaServer Pages(JSP)技术来构建动态网页,同时结合Struts和Hibernate这两个关键框架,为Java ...
第08章“初识Java EE经典MVC框架Struts”带领读者进入模型-视图-控制器(Model-View-Controller,MVC)设计模式的世界。Struts是Java Web开发中广泛应用的一个开源框架,用于构建基于JSP和Servlet的应用。在这一章,...
\ch01\* 第1章 初识JSP所有例程代码 \ch02\* 第2章 HTTP和Servlet基础 所有例程代码 \ch03\* 第3章 JSP基本语法所有例程代码 \ch04\* 第4章 JSP内置对象所有例程代码 \ch05\* 第5章 JavaBean在JSP中的应用所有...