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文档下载到电脑

精彩图片

热门精选

大家正在看

× 游客快捷下载通道(下载后可以自由复制和排版)

限时特价:7 元/份 原价:20元

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219