一、创建web项目后导入相关jar包:
二、文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation=" /web-app_2_5.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</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>
|
三、创建相应的Action(继承ActionSupport)以及前台表单页面(略...):
<%@ taglib prefix="s" uri="/struts-tags"%>
|
四、修改struts.xml文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ""> <struts> <package name="default" extends="struts-default"> <action name="HelloWorld" class="com.conking.HelloWorld"> <result>/jsp/helloWorld.jsp</result> </action> <action name="Login" class="com.conking.Login"> <result name="input">/jsp/login.jsp</result> <result name="success">/jsp/success.jsp</result> </action> </package> </struts>
|
五、properties配置文件,国际化package_zh_CN.properties
username=\u7528\u6237\u540D password=\u5BC6\u7801 requiredstring=${getText(fieldName)}\u4E0D\u80FD\u4E3A\u7A7A HelloWorld.message=Struts 2 \u542F\u52A8
|
六、Login-validation.xml 进行form表单验证,注意这个文件命名有约束,要跟Login类有相同前缀:
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "">
<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>
|
阅读(633) | 评论(0) | 转发(0) |