手机版

Tutorial_Create Struts 2 Application in Eclipse(5)

发布时间:2021-06-08   来源:未知    
字号:

Specify folder name resources and press Finish. Create a file ApplicationResources.properties under resources folder.

Copy following content in ApplicationResources.properties. ername= Username label.password= Password label.login= Login

The JSPWe will create two JSP files to render the output to user. Login.jsp will be the starting point of our application which will contain a simple login form with username and password. On successful authentication, user will be redirected to Welcome.jsp which will display a simple welcom

e message. Create two JSP files Login.jsp and Welcome.jsp in WebContent folder of your project. Copy following content into it.

Login.jsp<%@ page contentType="text/html; charset=UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html><head><title>Struts 2 - Login Application| </title></head><body><h2>Struts 2 - Login Application</h2><s:actionerror/><s:form action="login.action" method="post"><s:textfield name="username" key="ername" size="20"/><s:password name="password" key="label.password" size="20"/><s:submit method="execute" key="label.login" align="center"/>

</s:form></body></html>

Welcome.jsp

<%@ page contentType="text/html; charset=UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html><head><title>Welcome</title></head> <body> <h2>Howdy, <s:property value="username" />...!</h2></body></html>

Note that we have used struts2 <s:> tag to render the textboxes and labels. Struts2 comes with a powerfulbuilt-in tag library to render UI elements more efficiently.

The struts.xml file

Struts2 reads the configuration and class definition from an xml file called struts.xml. This file is loadedfrom the classpath of the project. We will define struts.xml file in the resources folder. Create file struts.xmlin resources folder.

Copy following content into struts.xml.

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <constant name="struts.custom.i18n.resources" value="ApplicationResources" /> <package name="default" extends="struts-default" namespace="/"> <action name="login" class="net.viralpatel.struts2.LoginAction"> <result name="success">Welcome.jsp</result> <result name="error">Login.jsp</result> </action> </package></struts>

Note that in above configuration file, we have defined Login action of our application. Two result paths are

Tutorial_Create Struts 2 Application in Eclipse(5).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
×
二维码
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)