`
youyun_2008
  • 浏览: 113586 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

spring mvc 实例

阅读更多
web.xml

<!--
		- Loads the root application context of this web app at startup, - by
		default from "/WEB-INF/applicationContext.xml". - Note that you need
		to fall back to Spring's ContextLoaderServlet for - J2EE servers that
		do not follow the Servlet 2.4 initialization order. - - Use
		WebApplicationContextUtils.getWebApplicationContext(servletContext) -
		to access it anywhere in the web application, outside of the
		framework. - - The root context is the parent of all servlet-specific
		contexts. - This means that its beans are automatically available in
		these child contexts, - both for getBean(name) calls and (external)
		bean references.
	-->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!--
		- Servlet that dispatches request to registered handlers (Controller
		implementations). - Has its own application context, by default
		defined in "{servlet-name}-servlet.xml", - i.e.
		"eventlog-servlet.xml". - - A web app can contain any number of such
		servlets. - Note that this web app has a shared root application
		context, serving as parent - of all DispatcherServlet contexts.
	-->
	
	<servlet>
		<servlet-name>abc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>
        <servlet-mapping>
		<servlet-name>abc</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>




abc-servlet.xml

<bean
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.Exception">uncaughtException</prop>
				<prop key="org.springframework.dao.DataAccessException">dataAccessException</prop>
				<prop key="org.springframework.transaction.TransactionException">dataAccessFailure</prop>
<!--				<prop key="com.rdps.eventlog.exception.EventLogException">eventLogException</prop>-->
			</props>
		</property>
	</bean>  

<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />



Controller:
@Controller
@RequestMapping("/xxx.do")
@SessionAttributes({"aaa","bbb"})
public class xxxController {

@RequestMapping(method = RequestMethod.GET)
public String setupForm(Model model)throws Exception {
		Event newEvent=generateDefaultEvent();
		AddEventDisplayData addEventDisplayData = new AddEventDisplayData(newEvent);
		setupFormCommon(model,addEventDisplayData);
		return "addEvent";
	}
	
	@RequestMapping(method = RequestMethod.GET,params = "actionType=fromTask")
	public String setupTaskForm(Model model, String taskId)throws Exception {
		Task task = RMSService.getTaskById(Integer.parseInt(taskId));
		return "addEvent";
	}
	
		
	@RequestMapping(method = RequestMethod.GET,params = "actionType=edit")
	public String setupEditForm(Model model, String eventId, String fromReport, HttpServletRequest request)throws Exception {
		List<EntityRow> typesList = RMSService.getTypesFromTeam(newEvent.getTeam().getId(),newEvent.getSource().getId());
                model.addAttribute("typesList", typesList);
		return "addEvent";

	}
	
	
	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(HttpServletRequest request,Model model,AddEventDisplayData addEventDisplayData,BindingResult result, SessionStatus status) throws Exception {
		
		validate(addEventDisplayData,result);
			
		if (!result.hasErrors()) {
		    generateEvent(addEventDisplayData, request);
		    Event event = addEventDisplayData.getEvent();
		    
		    if(event.getPreviousEventId()==null || event.getPreviousEventId().length()==0) {
		        RMSService.createEvent(event);
		        status.setComplete();
			    return "redirect:listEvent.do?message=msg.addEventLogSuccess";
		    }
		    else {
		    	RMSService.editEvent(event);
		    	String returnView= "redirect:listEvent.do?message=msg.modifyEventLogSuccess";
		    	if (request.getSession().getAttribute("fromReport") != null) {
		    		returnView="redirect:searchEvent.do?getResult=y&fromReport=y&message=msg.modifyEventLogSuccess";
		    	}
		    	status.setComplete();
			    return returnView;
		    }
		  
		}
		else {
			return "addEvent";
		}
		
	}
	
	
	
	/**
	 * get sources for Add Event page, in order that the new event's source can be
	 * selected.
	 * @return List
	 */
	@ModelAttribute("sources")
	public List<Source> getSources() {
		return RMSService.getSources();
	}
}



jsp


<form:form modelAttribute="addEventDisplayData" onsubmit="return checkData()">

<table class="func_table" id="main_table" width="900">
    <tr>
		<td class="table_title" colspan="2">
			<c:choose>
				<c:when test="${addEventDisplayData.edit}">
					Modify Event
				</c:when>
				<c:otherwise>
					Add New Event
				</c:otherwise>
			</c:choose>
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<table width="80%" cellspacing="0" cellpadding="2">
				<tr>
				    <td align="center" width="10%">Source</td>
					<td align="left" width="30%">
					
						<form:select path="event.source.id" onchange="getEventTypes()" cssStyle="width: 150px;">
							
							<form:options items="${sources}" itemValue="id" itemLabel="name"/>
						</form:select>
					</td>
					<td align="center" width="10%">Team</td>
					<td align="left" width="30%">
						
						<form:select path="event.team.id" onchange="getEventTypes(); getEventUsers(this)" cssStyle="width: 150px;">
							
							<form:options items="${teams}" itemValue="id" itemLabel="name" />
						</form:select>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
					<td nowrap align="center">Team member</td>
					<td id="teamMember">
						<table border="0">
							<tr>
								<td>
									<select multiple="true" id="allTeamUsers" style="width: 150px; height: 150px;">
									<c:forEach var="member" items="${teamMembersList}">
									 <option value="${member.id}">${member.name}</option>
									</c:forEach>
									</select>
								</td>
<td style="color: #333333;" width="80" align="center">
									<input id="add" name="add" type="button" value="&gt;" onclick="addUser()" class="button_small"><br><br>
									<input id="remove" name="remove" type="button" value="&lt;" onclick="removeUser()" class="button_small"><br><br>
									<input id="addAll" name="addAll" type="button" value="&gt;&gt;" onclick="addAllUsers()" class="button_small"><br><br>
									<input id="removeAll" name="removeAll" type="button" value="&lt;&lt;" onclick="removeAllUsers()" class="button_small">
	</td>
	<td>
									<form:select path="members" multiple="true" id="selectedUsers" cssStyle="width: 150px; height: 150px;">
									<c:forEach var="member" items="${selectedMembersList}">
									 <form:option value="${member.id}">${member.name}</form:option>
									</c:forEach>
									</form:select>
									
									<form:errors path="members" cssClass="font_error" />
									<label id="memberError" class="font_error" style="display:none"></label>
								</td>
							</tr>
						</table>
					</td>
分享到:
评论

相关推荐

    Spring MVC 实例

    **Spring MVC 框架详解** Spring MVC 是 Spring 框架的重要组成部分,它是一个用于构建 Web 应用程序的模型-视图-控制器(MVC)架构。Spring MVC 提供了灵活的处理机制,包括处理器映射、视图解析、数据绑定、本地...

    Spring Mvc实例

    总结来说,Spring MVC实例展示了如何通过注解驱动开发实现Web应用的路由控制、数据绑定、视图解析以及数据库操作。这个实例对于理解Spring MVC的工作原理和实际应用是非常有帮助的,也是Java Web开发者必备的技能之...

    Spring MVC实例 MVC注解配置

    Spring MVC 是一个强大的Java Web开发框架...综上所述,Spring MVC实例中的MVC注解配置大大简化了Web应用的开发流程,使得代码更加简洁、直观。通过熟练掌握这些知识点,开发者能够高效地构建出高质量的Java Web应用。

    学习Spring MVC,关于注解的Spring MVC,简单例子,关于控制器的Spring MVC,学习Spring,简单Spring MVC实例

    在Spring MVC实例中,通常会涉及到配置文件,如`spring-mvc.xml`,但现在更推荐使用Java配置。例如,你可以创建一个`WebConfig`类,然后使用`@Configuration`和`@EnableWebMvc`注解来启用Spring MVC: ```java @...

    Spring mvc 实例下载可运行

    在这个实例中,"Spring mvc 实例下载可运行" 提供了一个可以直接运行的Spring MVC项目,帮助开发者更好地理解和学习Spring MVC的实践操作。 1. **Spring MVC基本概念** - **模型(Model)**:存储业务数据的部分,...

    spring mvc实例

    总结来说,Spring MVC实例是一个绝佳的学习工具,它让你能够亲手实践MVC模式,理解Spring框架如何协调各个组件来处理Web请求。通过深入研究这个实例,你将掌握Spring MVC的核心概念,为今后的Web开发打下坚实的基础...

    Spring MVC实例的简单项目

    **Spring MVC 实例详解** Spring MVC 是 Spring 框架的一部分,它是一个用于构建 Web 应用程序的模型-视图-控制器(MVC)框架。这个实例项目旨在帮助我们理解如何在实际开发中运用 Spring MVC。让我们深入探讨一下...

    spring mvc实例项目

    学习这个实例项目,你可以深入理解Spring MVC如何处理请求、如何与ExtJS2进行数据交互,以及如何利用Spring JDBC进行数据库操作。同时,它也展示了MVC模式在实际项目中的应用,有助于提升你对Web应用开发的整体理解...

    Spring MVC实例

    **Spring MVC 实例详解** Spring MVC 是 Spring 框架的一部分,它是一个用于构建 Web 应用程序的轻量级、灵活的模型-视图-控制器(MVC)框架。这个框架提供了一种组织和解耦应用程序代码的方式,使得开发者可以更...

    spring MVC 实例代码

    基于注解的spring mvc,dao 层注解:@Repository("userDao"), entity层注解:@Entity,service层注解:@Service("userService"), action层注解:@Controller("userController") @RequestMapping("/user.do")

    Spring MVC 实例(附带详细讲解)

    在本实例中,我们将深入理解Spring MVC的基本概念,以及如何在一个实际项目中运用它。 首先,Apache Tomcat 是一个广泛使用的开源Java Servlet容器,它实现了Servlet和JavaServer Pages (JSP) 规范,是搭建Spring ...

    Spring MVC 基于注解实例

    Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...

Global site tag (gtag.js) - Google Analytics