我正在学习如何在google app引擎上编写java servlet和jsp页面.我正在尝试使用MVC模型,但我不确定我是否正确行事.目前,我有一个访问页面时调用的servlet.servlet执行所有处理并创建一个HomePageViewModel对象,该对象被转发到jsp,如下所示:
// Do processing here
// ...
HomePageViewModel viewModel = new HomePageViewModel();
req.setAttribute("viewModel", viewModel);
//Servlet JSP communication
RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("/jsp/home.jsp");
reqDispatcher.forward(req, resp);
Run Code Online (Sandbox Code Playgroud)
在jsp方面,我有这样的事情:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="viewmodels.HomePageViewModel" %>
<%
HomePageViewModel viewModel = (HomePageViewModel) request.getAttribute("viewModel");
pageContext.setAttribute("viewModel", viewModel);
%>
<html>
<body>
<% out.println(((HomePageViewModel)pageContext.getAttribute("viewModel")).Test); %>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
所以我的问题是双重的.首先,这是为小型webapp做事的合理方式吗?这只是我正在上课的一个小项目.第二,在jsp文件中,是否有更好的方法来访问viewmodel数据?
编辑:虽然答案是教我的东西,但我仍然不知道如何"实施"模型.任何人都可以举例说明我将如何创建一个简单的模型类,它具有一些基本函数,可以调用NSUserDefaults或JSON Web调用,以及如何从任何视图控制器访问它?
我刚刚开始进行iOS开发,并且我的应用程序需要一个Model来与控制器之间的整体数据流进行交互,但我不确定它们应该如何正确实现.
我知道View是在故事板中,而Controllers是与这些视图相关联的类.
为应用程序实现中央模型的正确方法是什么?我是否创建了一个类(即"MyModel.h/.m"),然后从我的所有视图控制器中导入它?
我也看到人们使用UINavigationController-> RootViewController作为他们的模型,这可行吗?
我搜索了这个问题并搜索了堆栈溢出了几个小时,但现在我正在寻求一个新的问题.
编辑:虽然答案是教我的东西,但我仍然不知道如何"实施"模型.任何人都可以举例说明我将如何创建一个简单的模型类,它具有一些基本函数,可以调用NSUserDefaults或JSON Web调用,以及如何从任何视图控制器访问它?
Views文件夹中有一个.
应用程序的根目录中还有另一个.
我想注册一个自定义处理程序,我不知道代码应该去哪里.我在集成模式下运行IIS7所以我必须添加一个<handlers>标签,<system.webServer>但是当我在Views文件夹中查看web.config时,我看到它使用了<httpHandlers>under <system.web>标签.
所以有两个问题:1.为什么mvc应用程序中有两个web.config文件?2.我应该在何处以何种方式注册我的自定义HTTP处理程序?
我最近看了Trygve Reenskaug在DCI上发表的以下演讲:https ://vimeo.com/43536416 这真让我大吃一惊 。嗯,在代码中看到软件的不同组件之间的交互是一个很有吸引力的想法。
我试图在javascript中找到DCI的示例,但未成功。然后我开始怀疑。DCI模式是否与事件编程模式相反?
我猜事件编程在javascript中是新潮,因为它允许解耦,并且因为经典继承概念不是js固有的。我想我了解事件编程的好处,但是我也注意到,当调试需要遵循事件消息时,调试可能会非常困难。
说两个概念都对吗正确吗?还是我弄错了?我错过了js中DCI的一些示例实现吗?为了理解这个概念,我应该看什么?
javascript model-view-controller design-patterns event-based-programming dci
我正在尝试创建一个非常简单的FreeMarker和Spring集成.但是,当我运行我的示例时,我得到一个例外:
GRAVE: Servlet.service() for servlet MyServlet threw exception
javax.servlet.ServletException: Could not resolve view with name 'Home' in servlet with name 'MyServlet'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1162)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
Run Code Online (Sandbox Code Playgroud)
我不知道配置有什么问题:
web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.jverstry.Configuration</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> …Run Code Online (Sandbox Code Playgroud) 我是春天的新手所以请耐心等待.
我看着这个指南(http://java2t.com/233/using-spring3-jdbctemplate-rowmapper-to-return-list-of-records/).它在eclipse中使用"普通"java项目.我正在尝试使用动态Web项目来模拟示例,但我没有取得任何成功.
我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<display-name>StaffDirectory</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Run Code Online (Sandbox Code Playgroud)
我的spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.flinders.staff.directory" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:view-controller path="/" view-name="index" />
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="PVHmg57bE4" />
</bean>
<bean …Run Code Online (Sandbox Code Playgroud) 直接离开我需要为一些冗长的帖子道歉,但这已经困扰了我很长一段时间了.我最近已经阅读了很多关于MVC的内容,以及它如何在Java的Swing世界中占有一席之地,我仍然无法理解为什么这在任何比教程提供的简单玩具示例稍微复杂一点的应用程序中甚至是非常有用的.但是让我从一开始就开始......
我在C#/ .Net 4.0中完成了所有的GUI编程,这个编程并不广泛但足以让我们更好地理解MVVM - 这是MVC的新版本.这是一个非常简单的概念:您使用XAML(类似于comnponents的描述的XML)定义GUI,指定例如表及其模型之间的绑定,文本字段的字符串值.这些绑定对应于您完全单独定义的对象属性.这样,您就可以在视图与世界其他地方之间实现完全脱钩.最重要的是,模型中的所有更改都"几乎"自动触发回相应的控件,事件驱动设计更加集中等等.
现在,回到Java,我们需要使用旧学校的MVC.让我从一个非常简单的例子开始:我正在尝试使用带有两个组合框和一个按钮的面板.在第一个组合中选择值将驱动第二个组合框的值,根据两个组合框中的值选择第二个组合框中的值将调用外部服务,并且该按钮将使用外部服务重置第一个组合中的值同样.如果我使用"我的"方法来做,我会按如下方式进行:
public class TestGUI {
private JComboBox<String> firstCombo;
private JComboBox<String> secondCombo;
private JButton button;
private ExternalReloadService reloadService;
private ExternalProcessingService processingService;
public TestGUI(ExternalReloadService reloadService, ExternalProcessingService processingService) {
initialise();
this.reloadService = reloadService;
this.processingService = processingService;
}
private void initialise() {
firstCombo = new JComboBox<>();
secondCombo = new JComboBox<>();
button = new JButton("Refresh");
firstCombo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String value = (String) ((JComboBox) e.getSource()).getSelectedItem();
reloadSecondCombo(value);
}
});
secondCombo.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public …Run Code Online (Sandbox Code Playgroud) 我有一个MVC项目,在根目录中添加了一个名为WCF的文件夹。在此文件夹中,我创建了一个名为的WCF服务CustomFunctions。当我尝试启动该服务时,出现以下错误:
错误:无法从
http://localhost/Viper/WCF/CustomFunctions.svc... 获取元数据。元数据包含无法解析的引用:
附加说明:
找不到类型'Viper.WCF.CustomFunctions',作为ServiceHost指令中的Service属性值提供,或在配置元素system.serviceModel / serviceHostingEnvironment / serviceActivations中提供。
我昨天遇到此错误,并花了一些时间在互联网上寻找答案。这使我对Web.config和Global.asax.cs进行了很多更改。昨天,它开始工作了,我停了下来。但是,当我今天早上回来时,它并没有再全部工作。在此期间,没有添加任何新内容,也没有更改任何代码。
我已经将以下内容添加到我的Web.config中:
<system.serviceModel>
<services>
<service behaviorConfiguration="WCFBehavior" name="Viper.WCF.CustomFunctions">
<endpoint address="" binding="wsHttpBinding" contract="Viper.WCF.ICustomFunctions">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/Viper/WCF/CustomFunctions/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" httpGetUrl=""/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
这给我Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // …Run Code Online (Sandbox Code Playgroud) 关于在类中使用全局函数的问题,我正在研究MVC系统.
下面的方法是类的一部分,它调用类中的方法,例如模型和视图我的问题是我在那里有一个全局函数,名为redirect()我将它设置在一个函数文件中,该文件已加载到我的bootstrap文件,因为所有控制器都可能需要访问它,这是一种不好的做法,或者我最好将其作为控制器类的方法,因为所有控制器都扩展了父控制器.
public function post($slug){
if(!$slug){ redirect('blog'); }
$data = $this->model->getPost($slug);
$this->view->render('blog/single', $data);
}
Run Code Online (Sandbox Code Playgroud)
或者静态类会更有意义吗?对于一个简单的重定向函数来说,它似乎有点过头了.
我有一个简单的应用程序,列出了联系人报告,在其中我创建了一个列表视图,从Mongolab获取数据.
在那里我还做了一个输入表单,在提交时在列表中创建一个新的联系人报告
我在控制器中使用的功能是从他们网站上的angular示例建模的:
app.factory('Contact',function($mongolabResource){
return $mongolabResource('contacts');
});
function ContactCreateCtrl($scope,$location,Contact) {
Contact.save(contact,function(){
$location.path('/');
});
};
Run Code Online (Sandbox Code Playgroud)
$ location.path()是重新加载页面的回调.
我如何重写这个,以便在提交数据时(.save()成功)视图重新加载而不重新加载页面?
我尝试删除然后重新定义数组但似乎没有工作:
Contact.save(contact,function(){
delete $scope.contacts;
$scope.contacts = Contact.query();
});
Run Code Online (Sandbox Code Playgroud)
我想在删除功能上实现这一点.有人能指出我能学到这些的地方吗?
非常感谢任何帮助
java ×2
javascript ×2
spring ×2
angularjs ×1
asp.net-mvc ×1
c# ×1
class ×1
cocoa-touch ×1
dci ×1
freemarker ×1
function ×1
global ×1
httphandler ×1
iis-7 ×1
ios ×1
javabeans ×1
jsp ×1
metadata ×1
model ×1
mvvm ×1
objective-c ×1
php ×1
servlets ×1
swing ×1
wcf ×1
web-services ×1