Spring Framework Page Direction
Background: I'm relatively new to Java/Spring and inherited a project built on them. We're moving to AWS Elastic Beanstalk which changed the location of the main page for JSON requests from:
www.mywebsite.com/myApp/myAppJsonService
to:
www.mywebsite.com/myAppJsonService
That worked fine- all the functions that come out the JSON requests (most of them) are working perfectly. I have another page that takes a teacher's uploaded quiz via HTML form submit and parses the data. The form used to point to:
www.mywebsite.com/myApp/controllers/importQuiz
so I changed it to:
www.mywebsite.com/controllers/importQuiz
The web.xml file has:
<servlet>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:context/Controllers.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcher</servlet-name>
<url-pattern>/controllers/*</url-pattern>
</servlet-mapping>
And the corresponding Controllers.xml code:
<bean id="importExamController" class="com.myapp.controllers.ImportExamController">
<property name="commandClass" value="com.myapp.objects.spring.FileUploadBean"/>
<property name="myappManager" ref="myappManager"/>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/importExam">importExamController</prop>
<prop key="/heartbeat">heartBeatController</prop>
</props>
</property>
</bean>
The way I read it, regardless of the preceding "myapp" in the URL, it should find "/controllers/" in the URL, look to the Controllers.xml file and find the "/importExam" and direct it to the "importExamContr开发者_高级运维oller". That's not happening. Clearly, there is a fault in my logic, and I can't seem to find it. Any help would be very much appreciated.
EDIT:
Doing some digging through the logs, I found:
INFO: WSSERVLET14: JAX-WS servlet initializing
Aug 27, 2011 7:21:06 PM com.sun.xml.ws.transport.http.servlet.WSServletDelegate doGet
SEVERE: caught throwable
ClientAbortException: java.net.SocketException: Broken pipe
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:373)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:327)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:396)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:385)
and it goes on for a while. To me it looks like that is confirming the idea that the data is trying to be sent to something that is incorrectly mapped. Let me know if this might mean something else or if it's just irrelevant.
First off, why in the world would you want controllers in your URL? Secondly, you have the right idea of how it should work, and you're probably correct about something not being configured correctly.
You should really read up on the enhanced MVC functionality in Spring 3.0+. With annotations and the mvc namespace, your code would look like (e.g.):
@Controller
@RequestMapping("importExam")
public class ExamController {
private final examService;
@Autowired
public ExamController(ExamService examService) {
this.examService = examService;
}
@RequestMapping(method=RequestMethod.GET)
public String getExams(ModelMap model) {
model.addAttribute("exams", examService.getExams());
return "exams";
}
}
And your controller is just:
<mvc:annotation-config/>
to load the controller and bind the request mappings to urls. This allows for wildcards too. The above controller would wire all URL request for /importExam to this controller, and process the default GET method at the base context. You could add nested methods with additional request mappings which would reside beneath the /importExam context.
精彩评论